How does PHP compare strings with comparison operators?

10,766

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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.

Share:
10,766
Shakti Singh
Author by

Shakti Singh

Updated on June 12, 2022

Comments

  • Shakti Singh
    Shakti Singh almost 2 years

    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.

  • coderabbi
    coderabbi over 11 years
    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.
  • Morufu Salawu
    Morufu Salawu over 11 years
    @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.
  • DrJonOsterman
    DrJonOsterman almost 11 years
    I believe you but is there any documentation in PHP.net where this is made explicit. So I may share for future reference.
  • Jorge Orpinel Pérez
    Jorge Orpinel Pérez about 10 years
    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).
  • Jorge Orpinel Pérez
    Jorge Orpinel Pérez about 10 years
    And actually casting (float) can still be a problem for example when comparing '1.8-a' to a later '1.8' ...
  • Andron
    Andron about 10 years
    Sure. There is an official documentation here: Comparison Operators and here: String conversion to numbers
  • orrd
    orrd almost 10 years
    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?
  • server_kitten
    server_kitten almost 10 years
    For version comparison, version_compare() works nicely.
  • hmundt
    hmundt over 8 years
    @orrd look in the first row of the second table / the table with the heading "Comparison with Various Types"
  • ToyRobotic
    ToyRobotic about 5 years
    The value of strings is based on the ASCII table. So it comes that lowercase letters will have a higher value than capital ones.