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, can be found in the PHP manual/Language Reference/Operator/comparison operator.
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 the Code code 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 the Code code 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 the Code code 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 the Code code 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.
The above describes the Tellmewhen string PHP is easy to ignore and error trap numbers and string comparisons, including the Tellmewhen string aspects of the content, I hope that the PHP tutorial interested in a friend helpful.