Home > Article > Backend Development > PHP empty() isset() is_null() Differences and Performance Comparison_PHP Tutorial
In php, the three functions empty() isset() is_null() are all used to determine whether it is empty. However, if I want to understand these three functions in detail, I will find that there are still many differences. Below I Let me summarize it for you.
is_null(), empty(), isset(), these functions and == ", == array() are often used in actual operations. Because the functions are very similar, their differences may be overlooked , if you are not careful, you will bring a lot of trouble to your work. These structures are listed below for your own and everyone’s reference. In view of the accuracy of the expression, some explanations are from the original English manual to avoid untimely updates of the Chinese manual. and issues such as improper translation
.is_null()
is_null(), bool, when the parameters meet the three conditions of null, is_null() will return TRUE.
null type, the following situations will be considered NULL:
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. Parameters can only be variables.
If the variable is not set, or the variable is unset()ed, or the variable value is NULL, return FALSE, otherwise return TRUE. That is, if it is not NULL, it falls into the category of isset, which is exactly the opposite of the is_null() function.
If multiple parameters are passed, the intersection will be taken. That is, TRUE is returned only when all parameters are consistent with isset().
ps:defined(), bool, used to check whether the constant is set.
source:http://cn2.php.net/manual/en/function.isset.php
empty()
empty(), bool, is mainly used to determine whether a variable is empty. Parameters can only be variables.
The following situations will be deemed empty:
The code is as follows | Copy code | ||||||||
0 (0 as an integer) 0.0 (0 as a float) “0″ (0 as a string) NULL FALSE array() (an empty array)
|
The code is as follows | Copy code |
$a;<🎜> $b = false;<🎜> $c = '';<🎜> $d = 0;<🎜> $e = null;<🎜> $f = array();<🎜> <🎜>?> empty() First is the var_dump output of empty: : var_dump(empty($a));<🎜> var_dump(empty($b));<🎜> var_dump(empty($c));<🎜> var_dump(empty($d));<🎜> var_dump(empty($e));<🎜> var_dump(empty($f));<🎜> ?> The program output is: bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) As can be seen from the code, empty() outputs true as long as the data type is empty or false. isset() Look at the output of isset: var_dump(isset($a)); 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) It can be seen that isset() can only be used to determine whether it is NULL and undefined. is_null() Finally is the output of is_null: var_dump(is_null($a)); 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 is literal.
It can be seen that empty() can be used to determine whether all data types are empty or false, while is_null is basically the same as isset and can only be used to determine whether it is NULL and undefined.
Summary summary of the differences between isset, empty, and 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.
The code is as follows
|
Copy code | ||||