Home >Backend Development >PHP Tutorial >Detailed explanation of the differences between empty, is_null and isset in php_PHP tutorial
There are many functions with similar functions in PHP, but there are subtle differences. As analyzed in this article, the three functions of is_null, empty, and isset are not easy to implement without a lot of effort. Got it! Let’s follow the webmaster to learn more about the differences between these three functions!
Let’s first take a look at the functional descriptions of these three functions
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 ("
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. Articles you may be interested in