In PHP empty (), Isset (), Is_null () Three functions are similar to many friends, but let's just think about it as if it were different, a null value, whether it was a variable, whether it was null, let me give an example.
Many people for PHP empty (), Isset () and Is_null () three functions of the use of fuzzy, the online data is also a lot, but may not be able to speak clearly, the following gives a test example, for understanding the difference between these three functions will have great benefits.
The types of tests are as follows:
$a;
$b = false;
$c = ";
$d = 0;
$e = null;
$f = Array ();
?>
Empty ()
The code is as follows |
Copy Code |
Var_dump (Empty ($a)); Var_dump (Empty ($b)); Var_dump (Empty ($c)); Var_dump (Empty ($d)); Var_dump (Empty ($e)); Var_dump (Empty ($f)); ?> Output BOOL (TRUE) BOOL (TRUE) BOOL (TRUE) BOOL (TRUE) BOOL (TRUE) BOOL (TRUE) |
As you can see from the code, empty () outputs true as long as the data type is empty or false.
Isset ()
The code is as follows |
Copy Code |
Var_dump (Isset ($a)); Var_dump (Isset ($b)); Var_dump (Isset ($c)); Var_dump (Isset ($d)); Var_dump (Isset ($e)); Var_dump (Isset ($f)); ?> Output BOOL (FALSE) BOOL (TRUE) BOOL (TRUE) BOOL (TRUE) BOOL (FALSE) BOOL (TRUE) |
You can see that isset () can only be used to determine if it is null and undefined.
Warning: isset () can only be used for variables, because passing any other parameter will result in parsing errors. To detect if a constant is set, use the defined () function.
You can use the Isset function when you want to determine if a variable has been declared
When you want to determine whether a variable has been given data and is not empty, you can use the empty function
When you want to determine if a variable exists and is not empty first isset function again with the empty function
For example, the detection of $id variables, when $id = 0 o'clock, with empty () and isset () to detect whether the variable $id is configured, both will return a different value--empty () think that there is no configuration, isset () can get the value of $id:
The code is as follows |
Copy Code |
$id = 0; Empty ($id)? print "It ' s empty.":p rint "it's $id."; Result: It ' s empty. Print " "; !isset ($id)? print "It ' s empty.":p rint "it's $id."; Result: It ' s 0. |
This means that when we use the variable handler function, when the variable may appear with a value of 0, use empty () to be careful, it is wiser to replace it with Isset.
When the URL trailing parameter of a PHP page appears id=0 (for example: test.php?id=0), try comparing:
The code is as follows |
Copy Code |
if (empty ($id)) $id = 1; -If id=0, the ID will also be 1 if (!isset ($id)) $id = 1; -If id=0, the ID will not be 1
|
The following code can be run separately to detect the above inference:
The code is as follows |
Copy Code |
if (empty ($id)) $id = 1; Print $id; Get 1
if (!isset ($id)) $id = 1; Print $id; Get 0 |
Is_null ()
The code is as follows |
Copy Code |
Var_dump (Is_null ($a)); Var_dump (Is_null ($b)); Var_dump (Is_null ($c)); Var_dump (Is_null ($d)); Var_dump (Is_null ($e)); Var_dump (Is_null ($f)); ?> Output BOOL (TRUE) BOOL (FALSE) BOOL (FALSE) BOOL (FALSE) BOOL (TRUE) BOOL (FALSE) |
Is_null literally means it.
This shows that empty () can be used to determine whether all data types are empty or false, and is_null is basically the same as isset, and can only be used to determine if null and undefined.
And then I'll attach a table to you.
The following table can clearly explain the relationship between them:
Variable Empty is_null isset
$a = "" True false True
$a =null true True false
var $a true true false
$a =array () True false true
$a =false true false true
$a =15 false False true
$a =1 false False true
$a =0 true false true
$a = "0" true false true
$a = "true" false false true
$a = "false" false false true
http://www.bkjia.com/PHPjc/445604.html www.bkjia.com true http://www.bkjia.com/PHPjc/445604.html techarticle in PHP empty (), Isset (), Is_null () Three functions are similar to many friends, but let's just think about it as if it's different, a null value, whether it's a variable, n ...