PHP is easy to ignore and error trap numbers are compared with strings. 0 returns true if it is compared with any character leading by a non-number (or character that cannot be converted to a number) (operator. the reason is that when comparing numbers with strings, first try to convert strings
0 returns true if it is compared with any character leading by a non-digit character (or character that cannot be converted to a number) (operator =.
The reason is that when a number is compared with a string, the string is first converted to a number before Comparison. a string that cannot be converted to a number is converted to 0. therefore, returns true when compared with 0.
More detailed comparison rules, multiple types of comparison rules, inPHP manual/language reference/operator/comparison operatorYou can find.
In PHP, when two numeric strings (strings containing only numbers) are compared, they are directly converted to numerical values for comparison.
Example: (note that the last two variables $ a and $ B are not equal)
The code is as follows:
// Example 1
$ A = '000000 ';
$ B = '000000 ';
If ($ a = $ B ){
Echo 'equal ';
} Else {
Echo 'notequal ';
}
?>
Running the above program finds that the result is equal (not the result we think)
Add a letter a to $ a and $ B respectively.
The code is as follows:
// Example 2
$ A = 'a0000203199106034578 ';
$ B = 'a0000203199106034579 ';
If ($ a = $ B ){
Echo 'equal ';
} Else {
Echo 'notequal ';
}
?>
The output is notEqual (correct result)
Example 1 is equal because PHP converts two numeric strings to a numeric string, and the two numbers are exactly the same.
The code is as follows:
$ A = 511203199106034578;
$ B = 511203199106034579;
Echo $ a; // output 5.1120319910603E + 17, that is, 511203199106030000
Echo $ B; // output 5.1120319910603E + 17, that is, 511203199106030000
?>
So the result we get in Example 1 is equal.
To avoid this unexpected result, use the type comparison operator ===( if $ a is equal to $ B and their types are the same)
The code is as follows:
// Example 4
$ A = '000000 ';
$ B = '000000 ';
If ($ a ===$ B ){
Echo 'equal ';
} Else {
Echo 'notequal ';
}
?>
In this way, we can get the expected notEqual.
Returns true if the http://www.bkjia.com/PHPjc/324586.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324586.htmlTechArticle0 is compared with a leading string (operator =) other than a number (or a character that cannot be converted to a number. the reason is that when the number is compared with the string, first try to convert the string...