When I used PHP for string comparison, I encountered a pit, as shown in the code:
<?php
var_dump('00E73694765433'=='0'); //true
var_dump('0134b40fsbi94u8'=='0'); //false
var_dump('0134b40fsbi94u8'=='134'); //false
echo PHP_EOL;
var_dump(is_numeric('00E73694765433'));//true
var_dump(is_numeric('0134b40fsbi94u8'));//false
?>
The first result is true, and the second one is false. After looking through the official documents, if the first one is forced to be converted to the number 0, then the second one should be forced to be converted to 134, but if it is converted to 134, the third one should be correct.
I printed two strings, and the result was very strange. I couldn’t figure it out after reading the official documents. Please give me some advice!
http://www.php.net/manual/zh/...
var_dump('0134b40fsbi94u8'=='134'); //false is comparative
Because they are all strings, we compare the first digit first. The comparison result of the first digit is a comparison between 0 and 1, so it is false
In addition, it is not recommended to use comparison operators to directly compare the sizes between strings. If you need to compare, php also provides some character comparison functions
To compare different types of data, it is best to use the congruent === and incongruent !== operators.
Because the comparison operators congruent === and incongruent !== will check the type:
In a word, when comparing, try to use === and!== instead of == and!=.
In addition, PHP also provides a series of type checking functions:
Some functions also provide type checking parameters, which need to be paid attention to, such as:
If the value of the third parameter is true, the function will also check whether the types are the same.
In addition, PHP also supports the comparison operators ==,!=,===,!== to compare whether two arrays or two objects are equal.
PHP also supports comparison operators (==,!=,===,!==) to determine whether two objects are equal:
Your first string exactly conforms to scientific notation.
For languages with loose type restrictions, there are often pitfalls like this in the news.
Try to use strictly typed checks
For weakly typed languages like php, if you can use
===
, don’t use it==