0 returns True if the leading string comparison (operator = = =) is associated with any non-numeric (or character that cannot be converted to a number).
The reason is that when a number is compared to a string, the first attempt is to convert the string to a number, then compare it to a string that cannot be converted to a number, and the result is 0, so that the comparison with 0 always returns TRUE.
More detailed comparison rules, multiple types of comparison rules,PHP Manual/Language Reference/Operator/comparison operatorcan be found.
In PHP, when comparing two numeric strings (strings with numbers only), they are converted directly into numerical values.
The following example: (Note $ A and $b the last of the two variables are not equal)
Copy CodeThe code is as follows:
Example 1
$a = ' 511203199106034578 ';
$b = ' 511203199106034579 ';
if ($a = = $b) {
echo ' equal ';
} else {
Echo ' notequal ';
}
?>
Run the above program and find the result is equal (not what we think)
We'll add a $ A to $b and a letter A to each one.
Copy CodeThe code is as follows:
Example 2
$a = ' a511203199106034578 ';
$b = ' a511203199106034579 ';
if ($a = = $b) {
echo ' equal ';
} else {
Echo ' notequal ';
}
?>
This time the output is notequal (the correct result)
Example 1 is equal because PHP converts two numeric strings into a digital type, and these two numbers are just equal to the following example
Copy CodeThe code is as follows:
$a = 511203199106034578;
$b = 511203199106034579;
echo $a; Output 5.1120319910603E+17 is 511203199106030000
Echo $b; Output 5.1120319910603E+17 is 511203199106030000
?>
So the result we got in Example 1 is equal.
The case for avoiding this unintended result is to use the type comparer = = = As in the following example (if $a equals $b and they are of the same type)
Copy CodeThe code is as follows:
Example 4
$a = ' 511203199106034578 ';
$b = ' 511203199106034579 ';
if ($a = = = $b) {
echo ' equal ';
} else {
Echo ' notequal ';
}
?>
So we can get the notequal we're expecting.
http://www.bkjia.com/PHPjc/324586.html www.bkjia.com true http://www.bkjia.com/PHPjc/324586.html techarticle 0 Returns True if the leading string comparison (operator = = =) is associated with any non-numeric (or character that cannot be converted to a number). The reason is that when a number is compared to a string, the first attempt is to move the string ...