this article to introduce to you about PHP strings and numbers compare some issues , because numbers with characters in PHP are different data types, there may be a lot of problems when you compare them.
Today you encounter a problem, call the other interface, follow the interface description, return to the number 0 1 2 Span style= "font-family: Arial" >0 marked success, others representing different error codes. The program passed if ($ret = = 0) to judge, start the procedure is good, today there is a problem, Because of the other side of the interface modification, directly return the letter string as an error message prompt, and then my side of the tragedy, the above judgment is always true
php php php 0 so if ($ret = = 0)
php Handbook / language reference / operator / Comparison operator You can find the
in the PHP two numeric strings in a ( string with numbers only ) The comparison is made by converting it directly to a numerical value.
The following example :( Note $a and the $b the last one of the two variables is unequal )
code is as follows |
copy code |
//example 1 <?php $a = ' 511203199106034578 '; $b = ' 511203199106034579 '; if ($a = = $b) { echo ' equal '; } else { Echo ' notequal '; } ? |
run the above program and find that the result is equal ( Not the result we think )
we put $a with the $b add one letter to each other a go in
code is as follows |
copy code |
//example 2 <?php $a = ' a511203199106034578 '; $b = ' a511203199106034579 '; if ($a = = $b) { echo ' equal '; } else { Echo ' notequal '; } ? |
this time the output is notequal ( correct result )
Example 1 to be Equal is because PHP convert two digital strings to digital type , and the two numbers are exactly the same as the following example
code is as follows |
copy code |
<?PHP&NBSP; $a = 511203199106034578; $b = 511203199106034579; echo $a;//output 5.1120319910603E+17 is 511203199106030000 Echo $b; Output 5.1120319910603E+17 is 511203199106030000 ? |
so we're in the example 1 The results obtained from the Equal
The case where this unintended result is avoided is the use of type-comparison characters === The following example ( if $a equals $b, and they are of the same type )
code is as follows |
copy code |
//example 4 <?php $a = ' 511203199106034578 '; $b = ' 511203199106034579 '; if ($a = = = $b) { echo ' equal '; } else { Echo ' notequal '; } ? |
so that we can get the expected notequal the
PHP strings and Numbers compare some questions