Can empty () replace isset () When php checks whether a variable is set ?. Let's first review empty and issetempty to check whether a variable is empty, 0, 0, NULL, FALSE, array (), var $ var; and objects without any attributes will be considered empty. for example, let's first review empty and isset.
Empty-check whether a variable is empty
"", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attribute will be considered empty. If var is empty, returns TRUE.
Isset-check whether the variable is set
Check whether the variable is set and not NULL. That is, if the variable is not set, false is returned. if the variable is NULL, false is returned.
The php manual explains clearly. if a variable is not set, what results will empty return?
[Php]
Var_dump (empty ($ undefined ));
Var_dump (empty ($ undefined); the result is: bool (true)
It can be seen that empty can also be used to check whether the variables are set. So should we use empty instead of isset?
I think isset should be used only to judge whether a variable is stored or not, because the meaning of isset is clear, that is, it is used to facilitate reading the code.
You can use
[Php]
If (! Empty ($ var) replaces if (isset ($ var )&&! Empty ($ var ))
If (! Empty ($ var) replaces if (isset ($ var )&&! Empty ($ var) when you want to determine whether a variable is NULL, you must not use isset.
Here we will introduce some content:
Performance of isset and empty?
In fact, the performance of isset and empty is very good, with little difference. One more point is that they are all statements, not functions. if you want to take a look at the specific content, you can refer to the article "differences between isset and is_null" by laruence.
The statement cannot be called by a variable function. In addition, in isset and empty, the return value of the function cannot be used as a parameter. for example, this cannot be done.
[Php]
Empty (intval ('3 '));
// Fatal error: Can't use function return value in write context
Empty (intval ('3 '));
// Fatal error: Can't use function return value in write context
However, the latest PHP5.5 release supports this feature, that is, it can use any expression in empty:
[Php]
Function always_false (){
Return false;
}
If (empty (always_false ())){
Echo "This will be printed. \ n ";
}
Function always_false (){
Return false;
}
If (empty (always_false ())){
Echo "This will be printed. \ n ";
}
Output:
This will be printed.
Receivempty checks whether a variable is NULL, 0, 0, NULL, FALSE, array (), var $ var, and objects without any attributes are considered empty, such...