Empty () determines whether a variable is null, and isset () determines whether a variable has been set. Just as the name implies, I took some detours at the beginning: When a variable value is equal to 0, empty () will also be True ), in this case, empty () is used to determine whether a variable is \ "null \", and isset () is used to determine whether a variable has been set. This so-called \ "as the name suggests \" makes me take some detours at the beginning: When a variable value is equal to 0, empty () will also be True ), as a result, some accidents may occur.
Script ec (2); script
Empty is used to determine whether the variable value is non-null or non-zero. Corresponding empty definitions include: "" (empty string), 0, "0", NULL, FALSE, array (), and $ var (declared only but not assigned values ). That is to say, when the value of the variable is the above, empty returns TRUE, and others return FALSE.
Isset checks whether the variable is set and is not NULL. Variable settings can be described in several aspects. 1: The simplest way is to declare and assign values to variables first; 2: Check whether the corresponding index or key exists in the array; 3: Check whether the corresponding attribute exists in the object.
From the two function definitions above, we can see that in some cases, the two can be shared, but the difference is still very large. In addition, they can only detect variables, and any non-variable detection will cause parsing errors. For example, if you directly check the return value of another function (empty (otherFunction (), you will see an error like "Fatal error: Can't use function return value in write context in.
In addition, isset can check multiple variables at a time, for example, isset ($ var1, $ var2, $ var3). When the isset values of these three values are TRUE, the result is TRUE, otherwise, the result is FALSE.
For example, if the $ id variable is detected and $ id = 0, empty () and isset () are used to check whether the variable $ id has been configured, the two will return different values -- empty () think that there is no configuration, isset () can get the value of $ id:
The Code is as follows: |
|
$ Id = 0; Empty ($ id )? Print "It's empty.": print "It's $ id ."; // Result: It's empty. Print" "; ! Isset ($ id )? Print "It's empty.": print "It's $ id ."; // Result: It's 0. |
This means that when we use a variable to process a function, when the variable may have a value of 0, we should be careful when using empty (). In this case, it is more wise to replace it with isset.
When the URL tail parameter of a php page shows id = 0 (for example, test. php? Id = 0), try to compare:
The Code is as follows: |
|
If (empty ($ id) $ id = 1;-if id = 0, the id is also 1 If (! Isset ($ id) $ id = 1;-if id = 0, id is not 1 |
Run the following code separately to detect the above inference:
The Code is as follows: |
|
If (empty ($ id) $ id = 1; Print $ id; // get 1 If (! Isset ($ id) $ id = 1; Print $ id; // get 0 Test code: $ Sep =" "; Echo 'test undeclared var empty :'; Var_dump (empty ($ var); // TRUE Echo $ sep. 'test undeclared var isset :'; Var_dump (isset ($ var); // FALSE $ Var; Echo $ sep. 'test declared var but no set value empty :'; Var_dump (empty ($ var); // TRUE Echo $ sep. 'test declared var but no set value isset :'; Var_dump (isset ($ var); // FALSE, the variable declaration is not assigned a value, the default value is NULL $ Var = NULL; Echo $ sep. 'test declared var and set value NULL empty :'; Var_dump (empty ($ var); // TRUE Echo $ sep. 'test declared var and set value NULL isset :'; Var_dump (isset ($ var); // FALSE, the variable declaration value is NULL $ Var1 = ''; $ var2 = '0'; $ var3 = 0; $ var4 = FALSE; $ var5 = array (); Echo $ sep. 'test'' empty :'; Var_dump (empty ($ var1); // TRUE Echo $ sep. 'test'0' empty :'; Var_dump (empty ($ var2); // TRUE Echo $ sep. 'test 0 empty :'; Var_dump (empty ($ var3); // TRUE Echo $ sep. 'test FALSE empty :'; Var_dump (empty ($ var4); // TRUE Echo $ sep. 'test array () empty :'; Var_dump (empty ($ var5); // TRUE Echo $ sep. 'test', '0', 0, FALSE, array () isset :'; Var_dump (isset ($ var1, $ var2, $ var3, $ var4, $ var5); // TRUE: Test undeclared var empty: bool (true) Test undeclared var isset: bool (false) Test declared var but no set value empty: bool (true) Test declared var but no set value isset: bool (false) Test declared var and set value NULL empty: bool (true) Test declared var and set value NULL isset: bool (false) Test ''empty: bool (true) Test '0' empty: bool (true) Test 0 empty: bool (true) Test FALSE empty: bool (true) Test array () empty: bool (true) Test '', '0', 0, FALSE, array () isset: bool (true) |