PHP uses a variety of functions to determine 0 and null

Source: Internet
Author: User
This article mainly introduces about PHP through a variety of functions to determine 0 and empty, has a certain reference value, now share to everyone, the need for friends can refer to

The judgment of the function on 0

   $cast _id = 0;    Var_dump (strlen ($cast _id));  1    var_dump (Empty ($cast _id));//True    Var_dump (isset ($cast _id));//true    var_dump (is_null ($cast _id)) ;//false

The judgment of Emptiness

    $cast _id = "";    Var_dump (strlen ($cast _id));  0    var_dump (empty ($cast _id));//True    Var_dump (isset ($cast _id));//true    var_dump (is_null ($cast _id ));//false

Add: The following is to introduce the PHP syntax in the 0 not equal to null-empty solution

Today's question is this: in the PHP statement, I want to judge a value greater than or equal to 0. I'm using ( $value !=null && $value >=0 ) and the returned result is empty, which is really weird.

Experiment Summary:

The PHP statements are as follows:

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

Results:

A:0B:C: 1 D:

To determine whether a value [the array may be empty, etc] is greater than or equal to 0, another method can be used: is_numeric($index) === true

$index =array_search ($url, $CONTENTOTHERSTR, true); A value greater than or equal to 0 is present if (is_numeric ($index) = = = True) {echo "$url existed." <br> "; }else{echo "$url Add." <br> "; Array_push ($contentOtherStr, $url); }

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

Summary: PHP statements slightly strange, from other programming languages to the students must be more cautious, pay attention to the inertial thinking and grammatical differences, ah, to avoid falling into the pit.

Other information:

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

This can be used echo gettype(''); and echo gettype(NULL); to print a look! 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 an assignment
= = Two equals is judged equal and only compares values, no comparison type
= = = Three equals equal to both the judgment value and the type
! = is not equal to the symbol, only the value is compared, regardless of type
!== non-congruent symbols, comparison values and types

So the empty string ('), false, NULL and 0 are values equal and the type is 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.

Related Article

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.