Use and difference of empty, isset, and is_null in PHP, issetis_null
1. empty usage
Bool empty (mixed var)
If var is a non-null 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, returns TRUE if var is null.
2. isset ()
Isset -- check whether the variable is set
Description
Bool isset (mixed var [, mixed var [,...])
If var exists, TRUE is returned; otherwise, FALSE is returned.
If unset () is used to release a variable, it will no longer be isset (). If you use isset () to test a variable that is set to NULL, FALSE is returned. Note that a NULL byte ("0") is not equivalent to the NULL constant of PHP.
Note: If the variable does not exist, neither isset () nor empty () will report an error. is_null () and is_numeric () will report an error.
How can we differentiate the following three elements in the array: [0, '', null? (1) Difference 0:
$a = 0;isset($a) && is_numeric($a) === true
(2) Difference''
$a = '';empty($a) && $a=== ''
(3) Difference null
$a = null;is_null($a);
In addition, when submitting a form, you may often need to check whether a variable exists. For example, $ _ REQUEST ['status'] = 0; empty ($ _ REQUEST ['status']) returns true, but isset ($ _ REQUEST ['status']) is not null.
3. is_null ():
Bool is_null (mixed $ var) (function definition in the official php.net Documentation)
If the following three conditions are met, is_null () returns TRUE. Otherwise, FALSE is returned.
1. It is assigned NULL.
2. It has not been assigned a value.
3. It is not defined. It is equivalent to unset (). After unset () is a variable unset (), isn't it not defined?
Let's look at some examples:
$myvar = NULL; var_dump(is_null($myvar)); // TRUE $myvar1; var_dump(is_null($myvar1)); // TRUE Notice: Undefined variable $num = 520; unset($num); var_dump(is_null($num)); //TRUE Notice: Undefined variable var_dump(is_null($some_undefined_var)); //TRUE Notice: Undefined variable $myvar = 0; is_null($myvar); // FALSE $myvar = FALSE; is_null($myvar); // FALSE $myvar = ''; is_null($myvar); // FALSE