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.