Compile the page in PHPProgramI often use the variable processing function to determine whether a variable value of the parameter at the end of the PHP page is null. At the beginning, I used to use the empty () function, but found some problems, therefore, use the isset () function instead.
As the name suggests, empty () determines whether a variable is "null", and isset () determines 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 have someDifferences:Empty also checks whether the variable is null 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, the two will return different values -- empty () think that there is no configuration, isset () can get the value of $ ID:
$ 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 page shows id = 0 (for example, test. php? Id = 0), try to compare:
If ( Empty ($ Id) $ ID = 1 ; - If the ID = 0 , The ID will also be 1
If ( ! Isset ($ Id) $ ID = 1 ; - If the ID = 0 , The ID is not 1
Run the following commands separately:CodeTest the above inference:
If ( Empty ($ Id) $ ID = 1 ;
Print $ ID; // Get 1
If ( ! Isset ($ Id) $ ID = 1 ;
Print $ ID; // Returns 0.
To talk about their connection, empty () and isset () are both variable processing functions. They are used to determine whether the variables have been configured, it is because of their great similarity in the Process of processing variables that they lack understanding of their relationships. Considering the functions empty () and isset (), we will confuse people and give them a different angle. The processing objects of empty () and isset () are similarUndefined variable,0,Empty string.
If the variable is 0, empty () returns true, and isset () returns true;
If the variable is a Null String, empty () returns true, and isset () returns true;
If the variable is not defined, empty () returns true, and isset () returns flase;
Empty () is described as follows:
Description: bool empty (mixed var)
Empty () Return False . In other words , "", 0, "0 ",Null,False, Array (), VAR $ VaR ; And objects without any properties will be considered empty. If VaR is empty, return True .
Isset () is described as follows:
Isset () check whether the variable is set
Description: bool isset (Mixed var [, mixed var [,...])
If var exists, return True Otherwise, return False .
If you have used unset () After a variable is released, it is no longer an isset () . If you use isset () Test Null Will return False . Note that Null Bytes ("\ 0") are not equivalent to PhP's Null Constant.
Warning:Isset () Can only be used Variable Because passing any other parameter will cause a parsing error. To detect Constant Set or not, available 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.