Home > Article > Backend Development > Differences between empty(), is_null(), and isset() functions in PHP_PHP Tutorial
empty(), is_null(), isset() truth table (difference)
Let’s first take a look at the functional descriptions of these three functions www.bKjia.c0m
isset determines whether the variable already exists. If the variable exists, it returns TRUE, otherwise it returns FALSE.
empty determines whether the variable is empty. If the variable is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty, and TRUE will be returned if the variable is empty.
is_null determines whether the variable is NULL
How about this? This explanation is generally used, but this explanation is already very confusing. Let’s analyze it with specific examples below!
From this we can find that as long as the variable is "" or 0, or false or null, empty will return true as long as these values are.
isset only determines whether the variable exists. As long as your variable is not null or unassigned, the return result will be true. If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("") is not equivalent to PHP's NULL constant.
And is_null is exactly the inverse result of isset. We can think of it as !isset, which is an inverse operation of isset.
From the above examples, we can also draw the following conclusions (which will be often used in programming in the future):
Assume $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;
Here is_null($j) is true (it can be understood that isset($j) is false, because the variable $j is not declared in advance)
Two other points to note are:
(1) empty() only detects variables, and detecting anything that is not a variable will result in a parsing error. In other words, the following statement will not work: empty(addslashes($name)).
(2) isset() can only be used for variables, because passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, use the defined() function.
Summary and summary of the differences between isset, empty, is_null
What I just introduced: checking variables and parameter types is the basis of the differences between these three functions, and it is also the most easily overlooked. I saw many articles comparing these three functions on the Internet. These are rarely covered. What I want to talk about next is the difference when both check existing variables.
Copy the code as follows
$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 the above simple test, we can generally know that when a variable exists: isset, empty, is_null detection, and get the value. There are more variables not exemplified above. In fact, the test found:
empty
If variable is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var, undefined; and objects without any properties will be considered empty, and TRUE will be returned if var is empty. .
isset
Returns TRUE if the variable exists (not NULL), otherwise returns FALSE (including undefined). The variable value is set to: null, and the return value is also false; after unsetting a variable, the variable is canceled. Note that isset handles NULL value variables specially.
is_null
Check whether the incoming value [value, variable, expression] is null. Only if one variable is defined and its value is null will it return TRUE. Others will return FALSE [An error will occur after undefined variables are passed in. ! 】.
For more details, please check: http://www.bKjia.c0m/phper/php/56681.htm