There's a pit ahead.
PHP development process, will inevitably encounter these four values: false,null,0, ", and will also compare the four values, and then do business code processing. A careless move, will step on the pit, affecting the correctness and security of data judgment, so that the code is not robust, to the test and operation of the program caused a lot of trouble.
Look at the following code:
$a = NULL; $b = "; $c = 0; $d = false;
echo ($a = = $b)? 1:0; Output 1
echo ($a = = = $b)? 1:0; Output 0
echo ($a = = $c)? 1:0; Output 1
echo ($a = = = $c)? 1:0; Output 0
echo ($b = = $c)? 1:0; Output 1
echo ($b = = = $c)? 1:0; Output 0
echo ($a = = $d)? 1:0; Output 1
echo ($a = = = $d)? 1:0; Output 0
Second, anti-crater strategy
= = The type is converted first, then compared, and = = = The type is first compared, if the type is different directly returns unequal.
Third, why there is a pit
First understand the types of these four values:
$a = NULL; $b = "; $c = 0; $d = false;
Echo GetType ($a); Output null
Echo GetType ($b); Output string
Echo GetType ($c); Output integer
Echo GetType ($d); Output Boolean
The original four values of the type is not the same! So the combination of anti-crater strategy is good understanding.
There's a hole in life, beware
In fact, in PHP variables are stored in the structure of the C language, ", Null,false are stored with a value of 0, wherein the struct has a zend_uchartype, such a member variable, he is used to save the type of the variable, and the" type is a string, The type of NULL is Null,false is Boolean.
Four, anti-crater tips
Expand your understanding of these four values:
$a = NULL; $b = "; $c = 0; $d = false;
echo isset ($a)? 1:0; Output 0
echo isset ($b)? 1:0; Output 1
echo isset ($c)? 1:0; Output 1
echo isset ($d)? 1:0; Output 1
Echo ' <br> ';
echo is_null ($a)? 1:0; Output 1
echo is_null ($b)? 1:0; Output 0
echo is_null ($c)? 1:0; Output 0
echo is_null ($d)? 1:0; Output 0
Echo ' <br> ';
echo empty ($a)? 1:0; Output 1
echo Empty ($b)? 1:0; Output 1
echo Empty ($c)? 1:0; Output 1
echo Empty ($d)? 1:0; Output 1
There is no pit in the world, there are many people in the pit.
Those pits that PHP stepped on (4) false,null,0, ' explanation