Isset-detect whether the variable is set
The PHP isset () is used to detect whether one or more variables are set and returns TRUE if the detected variable exists, or FALSE.
Example:
The code is as follows |
Copy Code |
<?php $var = 1; if (Isset ($var)) { Echo ' variable $var has been set '; } else { Echo ' variable $var has not yet been set '; } ?>
|
Run the example output:
Variable $var has been set
The elements in the array are also valid:
The code is as follows |
Copy Code |
<?php $a = array (' Test ' => 1, ' hello ' => NULL); Var_dump (isset ($a [' test ')];//TRUE Var_dump (isset ($a [' foo ')];//FALSE Var_dump (isset ($a [' hello '));//FALSE ' Hello ' equals NULL, so it is considered unassigned. If you want to detect NULL key values, try the bottom method. Var_dump (array_key_exists (' hello ', $a)); TRUE |
Attention
isset () can only be used to detect variables, and passing any other parameter will result in a parse error.
Isset () is a language structure and not a function, so it cannot be invoked by a variable function.
empty-Check if a variable is empty
Empty () returns False if the variable is non-empty or Non-zero
Empty () returns FALSE if Var is a non-null or Non-zero value. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any attributes will be considered empty and TRUE if Var is empty.
Empty () is the antonym of (Boolean) Var, except that no warning is generated when a variable does not have a value.
The code is as follows |
Copy Code |
<?php $var = 0;
The result is true because the $var is empty if (empty ($var)) { Echo ' $var is either 0 or not set at all '; } The result is false because the $var has been set if (!isset ($var)) { Echo ' $var is isn't set at all '; } ?> |
Example comparison
code is as follows |
copy code |
$is _var = ' ; If (isset ($is _var)) { echo variable exists! <br/> "; } else { echo variable does not exist! <br/> "; } If (empty ($is _var)) { echo variable is empty! <br/> "; } else { echo variable is not empty! <br/> "; } ? |