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 beforei
in alphabetical order so the test of>
(greater than) isfalse
- earlier in the order is considered 'less than' rather than 'greater than'.In the second example,
ia
comes afteri
alphabetical order so the test of>
(greater than) istrue
- 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.