Those pits that PHP stepped on (4) false,null,0, ' explanation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.