How does PHP compare strings with comparison operators?

PHP will compare alpha strings using the greater than and less than comparison operators based upon alphabetical order.

  • In the first example, ai comes before i in alphabetical order so the test of > (greater than) is false - earlier in the order is considered 'less than' rather than 'greater than'.

  • In the second example, ia comes after i alphabetical order so the test of > (greater than) is true - later in the order being considered 'greater than'.


To expand on coderabbi's answer:

It is the same type of logic as when you order by number in some applications and get results like the following:

  • 0
  • 1
  • 105
  • 11
  • 2
  • 21
  • 3
  • 333
  • 34

It's not based on string length, but rather each character in order of the string.