Php null value _ PHP null value judgment when using php to write a page program, I often use a variable processing function to determine whether a variable value of a 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, I switched to the isset () function instead of the problem. As the name suggests, empty determines whether a variable is "null" and isset determines whether a variable has been set. It is exactly this php null value __
PHP null value judgment
When using php to write page programs, I often use the variable processing function to determine whether the variable value of a parameter at the end of the php page is null. at the beginning, I used to use the empty () function, but some problems are found. Therefore, the isset () function is used 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 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:
??? $ 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 appears (for example, test. php? Id = 0), try to compare:
??? If (empty ($ id) $ id = 1;-if the id is 1
??? If (! Isset ($ id) $ id = 1;-if the id is not 1
Run the following code separately to detect the above inference:
??? If (empty ($ id) $ id = 1;
??? Print $ id; // Get 1
??? If (! Isset ($ id) $ id = 1;
??? Print $ id; // get 0