Comparison operators in PHP are a bit weird and easy to make mistakes. Here are the comparison rules:
1. When two characters are compared in size, the two characters are compared. ASCII code size of characters - this is easy to understand.
2. When two strings are compared in size, they start from the first character and compare the corresponding ASCII sizes. As long as they start from a certain corresponding position, the current position of one of the strings If the character is larger than the corresponding character in another string, the size of the two strings can be directly determined, such as 'ba'>'az' - everyone knows this.
Then comparing '10' and 'a', of course it is still the same. First compare '1' and 'a' ASCII code, 'a' is larger.
3. When a number is compared with a string/character, the system first tries to convert the string/character into an integer/floating point type, and then compares it. For example, '12bsd' is converted to 12. 'a' is converted to 0. It must be noted that at this time, the corresponding ASCII code value is not compared with the number.
In fact, for the same reason, the result of 'a'+10 is also 10.
And easy to ignore: 0 is compared with any string that cannot be converted into a number (operator is ==), both return true.
In the end, the following result will appear:
var_dump('1000000'<'a'); //result: boolean true var_dump('a'<1); //result: boolean true var_dump(1<'1000000'); //result: boolean true
It is still a little different from the conventional thinking. .
The most common way to compare whether two strings are equal is to use "===" to judge. As for the difference between it and "==", simply put, the former emphasizes the "identical" type and also requires The same; the latter requires "equal", and the same value is enough. Or use strcmp to judge, but this can tell you whether the two strings are equal, but it cannot tell you where they are different.
Generally, !=, == can be used to compare whether two objects are equal. The reason why they are two objects is because they are not necessarily all strings, they can also be integers, etc.
If you use !==,=== (you can see an extra equal sign) for comparison, the types of the two objects must be strictly equal to return true; otherwise, use ==,!=, the string will be automatically Convert to the corresponding type for comparison.
22 == "22"; // Return true
22 === "22"; // Return false
Because In this way, some unexpected "accidents" often occur in our program:
0 == "I love you"; // Return true
1 == "1 I love you";// Return true
There is a set of functions for string comparison in the PHP tutorial: strcmp, strcasecmp, strncasecmp(), strncmp(), they are all if the former is better than the latter If larger, an integer greater than 0 is returned; if the former is smaller than the latter, an integer less than 0 is returned; if the two are equal, 0 is returned. The principles of their comparison are the same as the rules of other languages.
strcmp is used for case-sensitive (i.e. case-sensitive) string comparison:
echo strcmp("abcdd", "abcde"); // Returns 1 (>0), comparison are "b" and "b"
strcasecmp is used for case-insensitive string comparison:
echo strcasecmp("abcdd", "abcde"); // Returns -1 (< 0), what is compared is "d" and "e"
strncmp is used to compare part of the string, starting from the beginning of the string. The third parameter is the length to be compared:
echo strncmp("abcdd", "abcde", 3); // Returns 1 (>0), comparing abc and abc
strncasecmp is used for case-insensitive comparison of part of the string from Comparison starts from the beginning of the string. The third parameter is the length to be compared:
echo strncasecmp("abcdd", "abcde", 3); // Returns 0, comparing abc and abc, since size is not distinguished write, so both are the same.
Another situation is that simply comparing string sizes cannot meet our predetermined needs. For example, normally 10.gif will be larger than 5.gif, but if you apply the above functions, it will return - 1 means 10.gif is better than 5.gif. For this situation, PHP provides two natural comparison functions strnatcmp, strnatcasecmp:
echo strnatcmp("10.gif" , "5.gif"); // Return 1 (>0)
echo strnatcasecmp("10.gif", "5.gif"); // Return 1 (>0)
In PHP, using == is unreliable. Even if the variables are of the same type, == being true does not mean that they are truly equal. Therefore, try to use === for comparison in PHP. Be sure to think twice before using ==. The operation of === in PHP is ZEND_IS_IDENTICAL. PHP will not try to change the type of the value. It is very strict and the comparison speed is faster. For example:
$aa = '88.';//多个点 $b = '88'; if($aa == $b) echo "相等";
得到的结果是相等。因为88是一个数字,php会自动转换成整型或者浮点型进行==比较,当然会相等; strcmp是强制转换成string类型比较,===是要求数值一样并且类型一样 88是int类型,88.是浮点类型,当然不一样。用恒等 === 则 不会得到相等的结论.用strcmp()也不会得到相等的结论.所以最好不要使用 == 作为字符串相等比较.改用strcmp();
对于俩个字符串, 会首先判断他们是否是numeric_string, 如果是,那么就会转换成整形来比较.. 那么什么是numeric string呢? 因为PHP不区分类型, 所以它采用一个策略, 当你的变量看起来是一个数字的时候, 那么她就认为这个变量是一个数字,然后转换为整形来比较。
The above is the detailed content of What is the difference between the idea of introducing PHP comparison operators and conventional comparison methods?. For more information, please follow other related articles on the PHP Chinese website!