PHP empty functions and isset functions are often used, and the frequency is not low, two have similar places, but in fact there is a big difference, it is very important to distinguish between the two strictly.
The empty function is used to detect whether a variable is empty, and the Isset function is used to detect whether the function is set and is null, which is an important distinction, and may not be well understood, as the following example illustrates.
First look at the Isset function
Official explanation of Isset: Determine if a variable is set and are NOT NULL//Determine if a variable is set and the variable is not NULL
bool Isset (mixed $var, [, Mixed $ ...]);
Example:
<?php
$str 1;
$str 2= "";
$str 3= "Hello";
Var_dump (Isset ($str 0));
Var_dump (Isset ($str 1));
Var_dump (Isset ($str 2));
Var_dump (Isset ($str 3));
The result is false false true
to analyze:
Isset Official Handbook written: bool Isset (mixed $val [, mixed $var ...]);
The first $str0 is not set and the result of isset detection is not set to return false, which is generally not in doubt.
The second $str1 is set, but there is no assignment, this is $str1 null, although the isset detection result is set the variable, but because the value is null and therefore false, this is an error-prone place because many people think that Isset only detects whether the variable is set, Now that it's set, it's definitely true, which is wrong, even if the variable is set, and if NULL is false.
The third detection result is present, and the variable $str2 is null, so the isset result is true, the fourth one is the same, so is true.
You can see that the isset detects whether the variable exists, is null, or False if there is no setting or the value is null, not true.
Isset can be detected with multiple variables
If you are detecting multiple variables, you must have all the variables present and not NULL to be true, otherwise false
For example, the $STR1 and $st2 of the above example use isset detection, false, and $STR2 and $STR3 are true.
unset variable, isset to False.
let's look at the empty function
Explanation of the Official Handbook: Determine whether a variable is empty//determine if a variable is not a null value
bool Empty (mixed $val)
Example:
<?php
$str 1=null;
$str 2= ';
$str 3=0;
$str 4=false;
$STR 5= ' abc ';
Var_dump (Empty ($str 0));
Var_dump (Empty ($str 1));
Var_dump (Empty ($str 2));
Var_dump (Empty ($str 3));
Var_dump (Empty ($str 4));
Var_dump (Empty ($str 5));
The result will be: True true to True true to true false
It may be misleading here, as the last $STR5 variable is the string ' abc ', and the rest is displayed as true, that is, empty to judge it to be empty, let's look at the official description of empty:
determine whether a variable is considered to be empty,a variable are considered if it does not exist or if it ' s value equals FALSE.
empty does not generate a warning if the variable is not does.
It's pretty clear, empty. The detection variable is whether the value is null, if it does not exist, or if the value is False, empty returns True, and if the variable does not exist, the empty function does not produce a warning
From the above explanation, we can see why the example above is the result.
$str 0 does not exist, the empty function does not produce an error, the value of the variable is NULL, so returns TRUE.
$str 1 exists, but the value is null and is cast to the bool value false, as explained above, and true if the value is false.
$str 2 exists, but the value is NULL, convert bool type to FALSE, return true,
$str 3 exists, but the value is 0, 0 is converted to bool type, FALSE, with the above explanation, and true if the value is false.
$str 4 exists, but the value is false and therefore true,
The empty function returns false $str 5 exists with a value of string ABC, a value that does not conform to NULL, or a value of false.
It can be observed from here, in fact, the empty function will judge the variables detected, the process of judging the value of the variable will be detected, if it is bool, according to the bool value, if not bool value is cast to bool value and further judge. Therefore, if the value of the variable converts to a bool value, the empty function returns True if the value of the variable is converted to the True,empty function to return FALSE.
The detailed explanation is as follows:
If the variable does not exist, the empty function casts the variable to the bool type, and converts it to the False,empty function to return true.
The empty function returns True if the variable exists, but is null, 0, FALSE, array (), and so on, which will get false in the procedure.
If a variable exists and the value of the variable converts the bool type to true, the empty function returns FALSE.
This way to interpret the empty function is much clearer.
comparison of isset and empty
As you can see from the above interpretation, empty () is definitely true if Isset () is false.
Isset is appropriate to detect whether a variable is set and is initialized, return FALSE if the variable is not set, or if it is set without initialization, and returns True.
empty is suitable to detect whether a variable is assigned and is not the initial default value, if the variable does not exist or if the value is the default value initialized, the variable is considered null, returns True, or false. Since PHP does not specify variable types, the use of empty is especially cautious, and it may result in unwanted results due to incorrect usage.
For example you want to know if a variable of a string type is not present, and is not a null value, if the variable has assignment, but the content of the assignment is 0 of the string, the empty is not get the answer, because the empty will automatically assume that the string 0 is the type int the default value of the initialization value 0, This conclusion is wrong, this situation should use isset to determine whether the variable is set, and is not initialized, and then judge the variable is not the initialization of the default value of the string "", so as to get the correct results.