How does PHP compare strings with comparison operators?
Asked Answered
H

4

16

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.

Heartburn answered 15/10, 2012 at 2:50 Comment(0)
T
24

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'.

Terrieterrier answered 15/10, 2012 at 2:57 Comment(5)
I believe you but is there any documentation in PHP.net where this is made explicit. So I may share for future reference.Ronironica
Sure. There is an official documentation here: Comparison Operators and here: String conversion to numbersIndefensible
I can't find any mention of alphabetic string comparison on the pages linked to in Andron's comment. Can someone quote where this is actually mentioned in the documentation?Workbag
@Workbag look in the first row of the second table / the table with the heading "Comparison with Various Types"Foliate
The value of strings is based on the ASCII table. So it comes that lowercase letters will have a higher value than capital ones.Fluoroscope
G
7

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.

Gaikwar answered 15/10, 2012 at 3:4 Comment(3)
This can pose a problem when comparing version "numbers" (usually stored as strings), for example '10-a' is considered < '8b' so in this case it may be better to cast to (float) first (assuming the version string can be cast).Youth
And actually casting (float) can still be a problem for example when comparing '1.8-a' to a later '1.8' ...Youth
For version comparison, version_compare() works nicely.Mcintosh
M
1

The < and > comparison operators in PHP will compare the first character of your string, then compare other characters that follows in the strings.

Therefore, your first expression ai (first string) and i (second string) a is the first character in the string compared with i as the first character in the second string with > will return false, and subsequently the second statement will return true due to the same reason.

However, if you really need to compare two longer string values with many characters, you may try using the substr_compare method:

substr_compare("abcde", "bc", 1, 2);

in this sample, you have your two strings to be compared, 1 is the offset start position, and 2 represents how many characters you want to compare to the right of those strings. -1 will means the offset start from the end of the first string. e.g. do something like this:

substr_compare("string1", "string2", 0, length);

also, consider using strcmp() also i.e. strcmp("string1", "string2", length) where length is number of character you want to compare from the two strings.

Monkeypot answered 15/10, 2012 at 3:21 Comment(2)
While you are correct that strcmp() is preferable, PHP will, in fact, compare ALL characters of a string, not just the first, SO LONG AS THEY ARE RELEVANT. In other words, if alphabetization can be established after the first character, comparison ends there. If, however, the first characters are equal, comparison moves to the second (third, fourth, etc., as necessary) characters. Test it. $result = 'aaaaaab' > 'aaaaaaa' ? true : false; will return true while its opposite, $result = 'aaaaaab' < 'aaaaaaa' ? true : false; will return false.Terrieterrier
@coderabbi, we are saying the same thing. i hit the submit answer button not knowing some of what i wrote has been cut off by my snail computer action. Your response draws my attention to the error. it has been edited, and i hope the owner can understand it more better now. thank you for bringing it to my attention.Monkeypot
S
1

When both strings are in number format, PHP will convert the strings to numbers and convert the values.

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

Reference: Comparison Operators

Stylite answered 2/12, 2019 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.