How to compare 2 strings alphabetically in PHP?

Solution 1:

Use strcmp. If the first argument to strcmp is lexicographically smaller to the second, then the value returned will be negative. If both are equal, then it will return 0. And if the first is lexicograpically greater than the second then a positive number will be returned.

nb. You probably want to use strcasecmp(string1,string2), which ignores case...

Solution 2:

You can compare both strings with strcmp:

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.