phpstringcompareoperatorstype-coercion

How does PHP compare strings with comparison operators?


I'm comparing strings with comparison operators.

I need some sort of explanation for the below two comparisons and their result.

if('ai' > 'i')
{
    echo 'Yes';
}
else
{
    echo 'No';
}

output: No

Why do these output this way?

if('ia' > 'i')
{
    echo 'Yes';
}
else
{
    echo 'No';
}

Output: Yes

Again, why?

Maybe I forgot some basics, but I really need some explanation of these comparison examples to understand this output.


Solution

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