Phpempty (), isset (), is_null () function usage instance. In php, empty (), isset (), and is_null () functions are similar to each other in many of our friends, but it seems that they are different. a null value, whether a function is a variable, whether the function is n in php empty (), isset (), and is_null () is similar to that of many friends, but let's just look at it as if it is different. a null value, whether it is a variable, and whether it is null will be used as an example.
Many people have vague usage of PHP empty (), isset (), and is_null () functions. There are also a lot of information on the Internet, but it may not be clear, the following is a test example, which will be of great benefit to understanding the differences between the three functions.
The test type is as follows:
$;
$ B = false;
$ C = '';
$ D = 0;
$ E = null;
$ F = array ();
?>
Empty ()
| The code is as follows: |
|
|
Var_dump (empty ($ ));
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) |
The code shows that empty () outputs true if the data type is null or false.
Isset ()
| The code is as follows: |
|
|
Var_dump (isset ($ ));
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) |
We can see that isset () can only be used to determine whether it is NULL or not.
Warning: isset () can only be used for variables, because passing any other parameter will cause a parsing error. To check whether a constant has been set, use the defined () function.
You can use the isset function to determine whether a variable has been declared.
You can use the empty function to determine whether a variable has been assigned data and is not empty.
To determine whether a variable exists and is not empty, use the isset function and the empty function.
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 |
Is_null ()
| The code is as follows: |
|
|
Var_dump (is_null ($ ));
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.
It can be seen that empty () can be used to determine whether all data types are NULL or false, while is_null is basically the same as isset and can only be used to determine whether it is NULL or undefined.
A table will be attached to you later.
The following table clearly describes the relationship between them:
Empty is_null isset
$ A = "true false true
$ A = null true false
Var $ a true false
$ A = array () true false true
$ A = false true
$ A = 15 false true
$ A = 1 false true
$ A = 0 true false true
$ A = "0" true false true
$ A = "true" false true
$ A = "false true
Functions such as struct (), isset (), and is_null () are similar to each other in many cases. However, let's take a simple look at the differences between a null value and a variable, whether it is n...