Home > Article > Backend Development > PHP determines whether the value is empty
I recently learned PHP, and I have some doubts about judging whether it is empty, and I would like to share the records with you.
empty
If variable is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var, undefined; and objects without any attributes will be considered empty. If var is empty, TRUE will be returned. . (Recommended learning: PHP Programming from Beginner to Master)
If e is undefined or the value is NULLL, e is undefined or the value is NULLL, e=NULL, it must be empty , that is, empty($e)=true;
If e is int type, e is int type, e=0, relative to the number, 0 represents zero, that is, empty($e)=true;
If e is string type, e is string type, e="", relative to string, "" means empty, that is, empty($e)=true;
If e is string type, e is string type, e="0", relative to string, "0" means zero, that is, empty($e)=true;
If e is bool Type, e is bool type, e=false, relative to true, false means empty, that is, empty($e)=true;
If e is array type, e is array type, a=array( ), relative to an array, data without elements is empty, that is, empty($e)=true;
isset
If the variable exists (non-NULL) Returns TRUE, otherwise returns FALSE (including undefined). The variable value is set to: null, and the return value is also false; after unsetting a variable, the variable is canceled. Note that isset handles NULL value variables specially.
is_null
Check whether the incoming value [value, variable, expression] is null. Only one variable is defined and its value is null. Returns TRUE. Others return FALSE [An error will occur after undefined variables are passed in! 】
In some cases, it is recommended to use isset to determine whether a variable is NULL.
But semantically speaking, "whether a variable has been displayed initialized" and "whether it is NULL" are different concepts. It is inappropriate to use isset in some scenarios, such as checking a function. Whether the return value is NULL.
You can use "==" and "===" at this time to determine whether they are NULL.
As for "==" and "===", their direct difference is still very big. For "==", it recognizes the empty string, and 0 and false are both NULL. For "===", it represents NULL only if a variable is really NULL.
In addition, compared to "isset", the performance of "===" is basically similar, or even better.
So to sum up the above, the best way to judge whether a variable is NULL is to use "===" directly, so that you don't have to hesitate between is_null and isset. In fact, the above conclusion is also consistent with False's judgment.
The above is the detailed content of PHP determines whether the value is empty. For more information, please follow other related articles on the PHP Chinese website!