A simple comparison between empty () and isset.
The code is as follows: |
Copy code |
<? Php $ Var = 0; // The result is true because $ var is empty. // The result is true because $ var = 0 If (empty ($ var) {echo '$ var is either 0 or not set at all ';} // The result is false because $ var has been set If (! Isset ($ var) {echo '$ var is not set at all ';} ?> |
The above empty and isset objects that are judged to have no attributes will be considered empty.
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: |
Copy code |
$ 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 |
Is_null
The code is as follows: |
Copy code |
$ 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 Variable = 0 or variable = 0 |
Example
The code is as follows: |
Copy code |
$ A = 0 $ a = "" $ A = 0 equals to $ a = "" |
When passing parameters in a php url
When the URL tail parameter of the php page shows id = 0 (for example, test. php? Id = 0), try to compare:
The code is as follows: |
Copy code |
If (empty ($ id) $ id = 1; // if id = 0, the id is also 1 If (! Isset ($ id) $ id = 1; // If id = 0, id will not be 1 Run the following code separately to detect the above inference: If (empty ($ id) $ id = 1; Print $ id; // Get 1 If (! Isset ($ id) $ id = 1; Print $ id; // get 0 |
Summary
In php, NULL and NULL are two different 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 the variable with the value of "NULL" as TRUE.
Other functions that may use php variable judgment
Isset ($ var): // whether the variable has been declared
Empty ($ var): // is the variable empty?
Defined ($ var): // whether the constant has defined define ()
Is_array ($ var): // checks whether the measurement variable is an array.
Is_null ($ var): // checks whether the measurement variable is null.