0 will return true when compared with any leading string that is not a number (or, in other words, a character that cannot be converted to a number) (the operator is ==).
The reason is that when comparing numbers with strings, try to first Convert the string to a number and then compare it. If a string cannot be converted to a number, the conversion result is 0. Therefore, comparing with 0 always returns true.
More detailed comparison rules, multiple types of comparison rules, in PHP You can find it in Manual/Language Reference/Operators/Comparison Operators.
In PHP, when two numeric strings (strings containing only numbers) are compared, they are directly converted into numerical values for comparison
The following example: ( Note that the last digits of the two variables $a and $b are not equal)
Copy the code The code is as follows:
//Example 1
$a = '511203199106034578';
$b = '511203199106034579';
if ($a==$b) {
echo 'equal';
} else {
echo 'notEqual';
}
?>
Copy the code The code is as follows:
//Example 2
$a = 'a511203199106034578 ';
$b = 'a511203199106034579';
if ($a==$b) {
echo 'equal';
} else {
echo 'notEqual';
}
?>
Copy the code The code is as follows:
$a = 511203199106034578;
$b = 511203199106034579;
echo $a; // Output 5.1120319910603E+17 which is 511203199106030000
echo $b; // Output 5.1120319910603E+17 which is 511203199106030000
?>
Copy the code The code is as follows:
//Example 4
$a = '511203199106034578';
$b = '511203199106034579';
if ($a===$b) {
echo 'equal';
} else {
echo 'notEqual';
}
?>
The above introduces the tellmewhen string PHP pitfalls that can easily be ignored and lead to errors. The comparison of numbers and strings includes the tellmewhen string aspect. I hope it will be helpful to friends who are interested in PHP tutorials.