Workaround for PHP syntax 0 not equal to null empty

Source: Internet
Author: User

Today do information collection page encountered a strange problem, study for a long time to find the root cause of the problem, it is a bit strange indeed.
(the younger brother often uses the C #, Java and other languages, PHP only occasionally in use, not research very deep). Please senior PHP programmer don't laughed at

The problem is this: in PHP statements (0!=null && 0 >=0), the returned result is empty, which is really very strange.


Experiment Summary:
The PHP statements are as follows:

$index = 0;

echo "A:". $index. "  <br> "; 0

echo "B:". ($index!=null && $index >=0). " <br> ";//
echo "C:". (Isset ($index) && $index >=0). " <br> ";//1

echo "D:". (0!=null). " <br> ";//

Results:

a:0
B:
C:1
D:
Visible: The result of 0!=null is empty. To make the results correct, you can modify the $index!=null to Isset ($index)

if (Isset ($index) && $index >=0))
{
echo "contains.";
}else{
echo "does not contain.";
}


This is very strange, finally solved. Mark, please.

Other information:

----------------------------------------------------------------------

The reason is that in PHP the variables are stored in the structure of the C language, and the empty string and null,false are stored with a value of 0, where the struct has a zend_uchar type; Such a member variable, he is used to save the type of the variable, while the type of the empty string is String,null type is Null,false is Boolean.

This can be used with echo GetType ("); and Echo GetType (NULL); Let's print it out! the = = = operator is not only the comparison value, but also the comparison type, so the third one is false!


Besides, in PHP,

= an equal sign is a value Assignment

== two equals are judged equal and only compare values, not comparison types

===  three equals equal to the judgment value and type

!=  Not equal to symbols, only compare values, regardless of type

!== non-congruent symbols, comparison values and types


so empty string ('), false, NULL and 0 are values equal and type different!

Attention:

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

3. "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties will be considered empty and return TRUE if Var is empty.

-----------------------------------------------------------------------


This address: http://blog.csdn.net/aerchi/article/details/42773459

Workaround for PHP syntax 0 not equal to null empty

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.