In php, empty () isset () is_null () functions are used to determine whether it is null, however, if I want to gain a detailed understanding of the three functions, there are still many differences between them. is
In php, empty () isset () is_null () functions are used to determine whether it is null, however, if I want to go into details about the three functions, there are still many differences.
Is_null (), empty (), isset (), these functions and = ", = array () are often used in actual operations, because the functions are similar, their differences may be ignored, which may cause a lot of trouble to the work. we will list these structures for your reference. In view of the accuracy of the statements, some explanations are from the original English manual to avoid problems such as untimely updates of the Chinese manual and improper translation.
Is_null ()
Is_null (), bool. if the parameter meets the three conditions of null, is_null () returns TRUE.
Null type, which is considered NULL in the following cases:
It has been assigned the constant NULL.
It has not been set to any value yet.
It has been unset ().
Source: http://cn2.php.net/manual/en/language.types.null.php
Isset ()
Isset (), bool, used to determine whether the parameter is set and not NULL. The parameter can only be a variable.
If no variable is set, the variable is unset (), or the variable value is NULL, FALSE is returned. otherwise, TRUE is returned. That is, if it is not NULL, it belongs to the category of isset. this is the opposite of is_null.
If multiple parameters are passed, the intersection is obtained. That is, TRUE is returned only when all parameters comply with isset.
Ps: defined (), bool, used to check whether a constant is set.
Source: http://cn2.php.net/manual/en/function.isset.php
Empty ()
Empty () and bool are mainly used to determine whether the variable is null. the parameter can only be a variable.
The code is as follows:
"" (An empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
Array () (an empty array)
Var $ var; (a variable declared, but without a value in a class)
Note:If the parameter is an unspecified variable, the variable is considered NULL and no error is reported. TRUE is returned.
But note that after 5.0.0, Objects with no properties are no longer considered empty.
Source: http://cn2.php.net/manual/en/function.empty.php
The method for determining whether it is null is as follows: = ", = array () and so on. there are limitations and there is nothing to say. The test type is as follows:
-
- $;
- $ B = false;
- $ C = '';
- $ D = 0;
- $ E = null;
- $ F = array ();
- ?>
Empty ()
The first is the var_dump output of empty:
-
- Var_dump (emptyempty ($ ));
- Var_dump (emptyempty ($ B ));
- Var_dump (emptyempty ($ c ));
- Var_dump (emptyempty ($ d ));
- Var_dump (emptyempty ($ e ));
- Var_dump (emptyempty ($ f ));
- ?>
- /*
- Program output:
- Bool (true)
- Bool (true)
- Bool (true)
- Bool (true)
- Bool (true)
- Bool (true)
- */
The code shows that empty () outputs true if the data type is null or false.
Isset ()
Let's take a look at the output of isset:
- Var_dump (isset ($ ));
- Var_dump (isset ($ B ));
- Var_dump (isset ($ c ));
- Var_dump (isset ($ d ));
- Var_dump (isset ($ e ));
- Var_dump (isset ($ f ));
- /* Output
- Bool (false)
- Bool (true)
- Bool (true)
- Bool (true)
- Bool (false)
- Bool (true)
- */
We can see that isset () can only be used to determine whether it is NULL or not.
Is_null ()
Finally, the output of is_null is as follows:
- Var_dump (is_null ($ ));
- Var_dump (is_null ($ B ));
- Var_dump (is_null ($ c ));
- Var_dump (is_null ($ d ));
- Var_dump (is_null ($ e ));
- Var_dump (is_null ($ f ));
- /* Output
- Bool (true)
- Bool (false)
- Bool (false)
- Bool (false)
- Bool (true)
- Bool (false)
- */
Is_null.
It can be seen that empty () can be used to determine whether all data types are NULL or false, while is_null is basically the same as isset and can only be used to determine whether it is NULL or undefined.
To sum up the differences among isset, empty, and is_null:
As described earlier: checking variables and parameter types are the basis for the differences between the three functions and are also the most overlooked, I have seen a lot of articles comparing these three functions on the Internet, which are rarely involved. next I want to talk about the differences in checking all the existing variables. the code is as follows:
-
- $ A = 100;
- $ B = "";
- $ C = null;
- // Isset check
- Echo "isset", "$ a = $ a", isset ($ )? "Define": "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 = $ ",! Emptyempty ($ )? "No empty": "empty", "rn ";
- Echo "empty", "$ B = $ B ",! Emptyempty ($ B )? "No empty": "empty", "rn ";
- Echo "empty", "$ c = $ c ",! Emptyempty ($ c )? "No empty": "empty", "rn ";
- Unset ($ B );
- Echo "empty", "$ B ",! Emptyempty ($ B )? "No empty": "empty", "rn ";
- $ B = 0;
- Echo "rnrn ";
- // Is_null check
- Echo "is_null", "$ a = $ ",! Is_null ($ )? "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 ";