The judgment variable is null and is used in many occasions. At the same time, you and many new users often make some mistakes, so I sorted out some common PHP methods to judge whether the variables are empty.
The judgment variable is null and is used in many occasions. At the same time, you and many new users often make some mistakes, so I sorted out some common PHP methods to judge whether the variables are empty.
1. isset
Function: determines whether a variable is initialized.
Note: It does not determine whether the variable is null and can be used to determine whether the elements in the array have been defined.
Note: When isset is used to determine whether the array element is initialized or not, it is about 4 times more efficient than array_key_exists.
The Code is as follows:
$ A = '';
$ A ['C'] = '';
If (! Isset ($ a) echo '$ a is not initialized '."";
If (! Isset ($ B) echo '$ B is not initialized '."";
If (isset ($ a ['C']) echo '$ a has been initialized '."";
// The result is
// $ B is not initialized
// $ A has been initialized
2. empty
Function: checks whether the variable is "null"
Note: Any uninitialized variable, variable with a value of 0 or false or null String "or null, empty array, object without any attribute, Hong Kong virtual host, empty = true
Note 1: uninitialized variables can also be detected as "null" by empty"
NOTE 2: empty can only detect variables, but cannot detect statements.
The Code is as follows:
$ A = 0;
$ B = '';
$ C = array ();
If (empty ($ a) echo '$ a is blank '."";
If (empty ($ B) echo '$ B is blank '."";
If (empty ($ c) echo '$ c is blank '."";
If (empty ($ d) echo '$ d is blank '."";
3. var = null
Function: determines whether the variable is "null"
Note: variables and empty arrays whose values are 0, false, or null strings "or" null "are determined to be null.
Note: A significant difference from empty is that when the variable is not initialized, var = null will report an error.
The Code is as follows:
$ A = 0;
$ B = array ();
If ($ a = null) echo '$ a is blank '."";
If ($ B = null) echo '$ B is blank '."";
If ($ c = null) echo '$ B is blank '."";
// The result is
// $ A is empty
// $ B is empty
// Undefined variable: c
4. is_null
Function: checks whether the variable is "null"
Note: When the variable is assigned "null", the detection result is true for the U.S. space.
Note 1: null is case insensitive: $ a = null; $ a = NULL no difference
Note 2: The check result is true only when the value of the variable is "null". The values 0, null String, false, and empty array are both false.
Note 3: The program reports an error when the variable is not initialized.
The Code is as follows:
$ A = null;
$ B = false;
If (is_null ($ a) echo '$ a is null '."";
If (is_null ($ B) echo '$ B is null '."";
If (is_null ($ c) echo '$ c is null '."";
// The result is
// $ A is NULL
// Undefined variable: c
5. var = null
Function: checks whether the variable is "null", a VM, and the variable type must be "null"
Note: When a variable is assigned "null" and the variable type is "null", the detection result is true.
Note 1: when it is determined to be "null", all functions are the same as is_null.
NOTE 2: When the variable is not initialized, the program will report an error summary.
In PHP, "NULL" and "NULL" are two concepts. Isset is mainly used to determine whether a variable has been initialized.
Empty can be used to set TRUE values for "false", "NULL", "0", "NULL", and "uninitialized" variables.
Is_null only determines the variable with the value of "NULL" as TRUE.
Var = null. All variables with "false", "NULL", "0", and "null" are regarded as TRUE.
Var = null only determines whether a variable with the value of "NULL" is TRUE. Therefore, when determining whether a variable is actually "NULL", is_null is mostly used, in this way, the equivalent interference of "false" and "0" is avoided.