Empty (), Is_null (), isset () Truth-table (difference)
Let's take a look at the functional descriptions of these 3 functions www.111cn.net
Isset determines whether a variable already exists, returns TRUE if the variable exists, or returns FALSE.
Empty determines whether the variable is empty, and if the variable is non-null or Non-zero, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any attributes will be considered empty and TRUE if the variable is empty.
Is_null determine if the variable is null
How, the general is this explanation, but this explanation has been very confusing, the following combination of concrete examples to analyze it!
From this we can see that as long as the variable is "" or 0, or false and null, all of these values empty return true.
Isset only determines whether a variable exists, so long as you are not NULL or unassigned, the return result is true. If you use Isset () to test a variable that is set to NULL, it returns FALSE. Also note that a null byte ("") is not equivalent to PHP's null constant.
And Is_null is just the isset of the inverse result, we can think of it as a!isset, is a isset reverse operation.
From the above examples, we can also draw the following conclusions (often used in future programming OH):
Suppose $var is any type
When empty ($var) is true, (bool) ($var) is false. Vice versa.
When Is_null ($var) is true, Isset ($var) is false. Vice versa.
For example:
$i = $j +1;
The Is_null ($j) Here is true (it can be understood that because Isset ($J) is false because there is no prior declaration $j this variable)
Additional two points to note are:
(1) empty () only detects variables, and detecting anything that is not variable will result in parsing errors. In other words, the following statement will not work: Empty (Addslashes ($name)).
(2) Isset () can only be used for variables, because passing any other parameter will result in a parse error. To detect if a constant is set, use the defined () function.
sum up the isset,empty,is_null difference
Just introduced: check variables, and parameter types, which is the basis for the difference between these 3 functions, and is the most easily overlooked. See there are a lot of comparisons on this 3 functions on the web. These are rarely involved. What I'm going to say here is the difference between checking for variables that already exist.
The code is as follows |
Copy Code |
<?php $a = 100; $b = ""; $c =null; //isset Check echo "Isset", "$a = $a", isset ($a)? " Define ":" Www.111cN.net undefine "," RN "; echo "Isset", "$b = $b", Isset ($b)? " Define ":" Undefine "," RN "; echo "Isset", "$c = $c", Isset ($c)? " Define ":" Undefine "," RN "; Unset ($b); echo "Isset", "$b", Isset ($b)? Define ":" Undefine "," RN "; $b = 0; echo "Rnrn"; //empty check echo "Empty", "$a = $a",!empty ($a)? " No empty ":" Empty "," RN "; echo "Empty", "$b = $b",!empty ($b)? " No empty ":" Empty "," RN "; echo "Empty", "$c = $c",!empty ($c)? " No empty ":" Empty "," RN "; Unset ($b); echo "Empty", "$b",!empty ($b)? No empty ":" Empty "," RN "; $b = 0; echo "Rnrn"; //is_null check echo "Is_null", "$a = $a",!is_null ($a)? " No null ": null", "RN"; echo "Is_null", "$b = $b",!is_null ($b)? " No null ": null", "RN"; echo "Is_null", "$c = $c",!is_null ($c)? " No null ": null", "RN"; Unset ($b); echo "Is_null", "$b", Is_null ($b)? No null ': ' null ', ' RN ';
|
Through this simple test, we can generally know that when a variable exists: isset,empty,is_null detection, get the value of the situation. There are no examples of more variables. In fact, the test found:
Empty
If the variable is non-null or Non-zero, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var, undefined; and objects that do not have any attributes will be considered empty and TRUE if Var is empty.
Isset
Returns TRUE if the variable exists (not null), or FALSE (including undefined). Variable value is set to: null, the return is also false;unset a variable, the variable was canceled. Note that isset for null value variables, special handling.
Is_null
Detects whether the passed-in value "value, variable, expression" is null, has only one variable defined, and its value is NULL, it returns TRUE. All other returns FALSE "undefined variable passed in error!" 】.