Empty checks whether a variable is "null", and isset checks whether a variable has been set. This so-called "name implies" 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. Originally, although empty and isset are both variable processing functions, they are used to determine whether the variables have been configured, but they are different: empty also checks whether the variables are empty and zero. When the value of a variable is 0, empty considers this variable to be null, that is, it is equivalent to not being set.
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. Different values will be returned for both -- empty considers 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 "<br> ";
! 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 Tutorial 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
View instances
The code is as follows:
<? Php
$ Var = 0;
// The result is true because $ var is null.
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 ';
}
?>
Because this is a language structure rather than a function, it cannot be called by variable functions.
Note: empty () only detects variables, and any non-variables can cause parsing errors. In other words, the following statement does not work: empty (addslashes ($ name )).
The following is the code of a detailed example of isset and empty functions tested by the script house. After reading this, it is almost the same.
The code is as follows:
<? Php
Error_reporting (e_all );
Echo '<B> undefined $ var </B> <br> ';
Echo "isset test: <br> ";
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <br> ';
}
Else
{
Echo 'variable $ var value is not empty <br> ';
}
Echo "direct test of variables: <br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
Echo '---------------------------------- <br> ';
Echo '<B> $ var = ''</B> <br> ';
Echo "isset test: <br> ";
$ Var = '';
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <br> ';
}
Else
{
Echo 'variable $ var value is not empty <br> ';
}
Echo "direct test of variables: <br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
Echo '---------------------------------- <br> ';
Echo '<B> $ var = 0 </B> <br> ';
Echo 'isset test: <br> ';
$ Var = 0;
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <br> ';
}
Else
{
Echo 'variable $ var value is not empty <br> ';
}
Echo "direct test of variables: <br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
Echo '---------------------------------- <br> ';
Echo '<B> $ var = null </B> <br> ';
Echo 'isset test: <br> ';
$ Var = null;
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <br> ';
}
Else
{
Echo 'variable $ var value is not empty <br> ';
}
Echo "direct test of variables: <br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
Echo '---------------------------------- <br> ';
Echo '<B> $ var = "php" </B> <br> ';
Echo 'isset test: <br> ';
$ Var = "php ";
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <br> ';
}
Else
{
Echo 'variable $ var value is not empty <br> ';
}
Echo "direct test of variables: <br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
?>