Difference between 0, "", null and false in php

Source: Internet
Author: User
: This article mainly introduces the differences between 0, null, and false in php. For more information about PHP tutorials, see. Many php users do not understand the differences between 0, "", null and false in php. these differences sometimes affect the correctness and security of data judgment and cause a lot of trouble for program testing.
Let's take a look at an example:

$ Str1 = null;

$ Str2 = false;

Echo $ str1 = $ str2? 'Equality ': 'Unequal ';

$ Str3 = "";

$ Str4 = 0;

Echo $ str3 = $ str4? 'Equality ': 'Unequal ';

$ Str5 = 0;

$ Str6 = '0 ';

Echo $ str5 ===$ str6? 'Equality ': 'Unequal ';

$ Str7 = 0;

$ Str = false;

Echo $ str7 = $ str8? 'Equality ': 'Unequal ';

?>
Running result:

// Equal, equal, not equal, equal.
The reason is that the variables in PHP are stored in a C-language struct. both NULL and NULL, and false are stored in a value of 0. The struct contains a member variable such as zend_uchartype, it is used to save the variable type, while the empty string type is string, the NULL type is NULL, and false is boolean.

Echo gettype (''); and echogettype (NULL); can be used to print this point! The ===operator is not only a comparison value, but also a comparison type. Therefore, the third operator is false!
So it can be said that = is equal to the following function:

Functioneq ($ v1, $ v2 ){
If ($ v1 = $ v2 & gettype ($ v1) = gettype ($ v2 )){
Return1;
} Else {
Return0;
}
}

Therefore, NULL strings (''), false, and NULL and 0 are of the same value and different types!

Note:

NULL is a special type.
NULL in both cases
1. $ var = NULL;
2. $ var;

3. "", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attribute will be considered empty. If var is empty, returns TRUE.

The above describes the differences between "0", "null", and "false" in php, including some content. I hope my friends who are interested in the PHP Tutorial will be helpful.

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.