This article mainly introduces a picture to clarify the difference between isset and empty functions. It has certain reference value. Now I share it with you. Friends in need can refer to it.
If you are in an interview and the interviewer happens to ask this question. You can answer like this:
If the variable value is 0, empty string, empty array, etc., empty thinks it is empty, while isset thinks it is not empty.
#If the variable does not exist, both isset and empty consider it to be empty.
#It is recommended to use the isset function to avoid pitfalls when using empty.
If you want to know the more detailed differences between them, please continue reading...
Drew a picture using ProcessOn
Note:
For the convenience of comparison, the empty function is inverted here . Note that
!empty()
- ##"", 0, "0", FALSE, array()... This condition is not completed, you can test it yourself
If you seem confused, please skip this section for now
This is the return result of isset empty corresponding to different variables. The difference between the two is gathered in this picture. The performance of
isset in the green dotted box, and the performance of
!empty in the yellow dotted box. It can be seen that empty needs to judge more conditions than isset to determine the return result.
Analogize variables to mineral water bottles, corresponding to 4 states:
- Water bottle: There is water in the bottle
- Air bottle: The water is finished, the bottle There is only air inside
- Vacuum bottle: The bottle was taken and vacuumed, but there was nothing inside
- There is no bottle on the table
Elementary school students empty and middle school students issetSuppose we can use isset empty The function needs to determine whether a mineral water bottle is empty. The result will be as follows:
- General value: The storage unit contains general values
- Air value: variables are "", 0, "0", FALSE, array()...
- Vacuum value: variables are NULL
- Variable does not exist or is not defined
And isset is a middle school student who has studied physics and understands the concept ofNow we can know that if we use these two functions to judge variables, the result will be like this:"vacuum"-he knows that there is still air in the bottle!
So in the first of the three situations above, the veteran driver isset has rich experience and came to a different conclusion than the elementary school student empty.
If it is an air value ( " ", 0, "0", FALSE, array()...), then empty thinks it is empty, and isset thinks it is not empty
If it is a vacuum value (NULL), then empty isset considers it to be empty
If it is a general value (except for these two cases, that is to say There is water in the bottle), then the empty isset thinks it is not empty
If the variable exists and its value is NULL, it also returns FALSEMore instructions:
If the variable exists and its value If it is not NULL, TRUE will be returned.
When checking multiple variables at the same time, TRUE will be returned only when each single item meets the previous requirement, otherwise the result will be FALSE
Use unset( ) After releasing the variable, it will no longer be isset().
PHP function isset()
can only be used for variables , passing any other parameters will cause a parsing error. To detect whether a constant has been set, use the defined() function.
Function: Check whether a variable is empty
Return value:
If the variable does not exist, return TRUE
If the variable exists and its value is "", 0, "0 ", NULL, FALSE, array(), var $var and objects without any attributes, return TURE
If the variable exists and the value is not "", 0, "0", NULL, FALSE, array(), var $var and objects without any attributes, return FALSE
More explanation:
empty() return value =! (boolean) var, but no warning message will be generated because the variable is undefined . See Converting to Boolean for more information.
empty() can only be used for variables . Passing any other parameters will cause a Paser error and terminate the operation.
To detect whether a constant has been set, use the defined() function.
Now we put mineral water and variables together and draw the most complete difference diagram:
If you can understand the previous content, You will thoroughly understand these two pictures, and the difference between isset empty will be engraved in your mind. If someone still asks you about the difference between the two, immediately throw out this picture~
##If you are interested, you can read on, the examples given in the official manual...PHP Manual
This is a Zhang will show the difference between empty isset if($var). From the table we can find:
empty($x) and isset($x) will not report any exceptions
The above is the detailed content of A picture to clarify the difference between isset and empty functions. For more information, please follow other related articles on the PHP Chinese website!