This article mainly introduces about a picture to clarify the difference between isset and empty function, has a certain reference value, now share to everyone, the need for friends can refer to
A brief talk about the difference between isset and empty?
If you are interviewing, it happens that the interviewer has raised the issue. You can answer this:
If the value of the variable is 0, an empty string, an empty array, and so on, empty considers it to be empty, and isset that it is not empty.
If the variable does not exist, both isset and empty think it is empty.
It is recommended to use the Isset function to avoid empty holes.
If you want to know more about the difference between them, please keep looking down ...
A picture that is completely clear
I drew a picture with ProcessOn.
Note :
For comparison purposes, the empty function is reversed. Attention is!empty()
"", 0, "0", FALSE, Array () ... The condition is not complete and can be tested on its own
If you look confused, please skip this section for a moment.
This is the result of the isset empty corresponding to the different variables, the difference between the two are gathered in this picture.
The expression in the green dashed box isset
, the yellow dashed box is !empty
the performance. As can be seen, empty compared to isset need to judge more conditions, in order to determine the return result.
If you don't understand a little bit, or can't remember all the details. Let me tell you how to remember all the contents of this picture!
Thinking of mineral water
Imagine that there is a water bottle on the table now. The bottle may have these 4 conditions:
Aquarius: There's water in the bottle
Air bottle: Water is finished, there is only air inside the bottle
Vacuum bottle: The bottle takes a vacuum, and there's nothing inside.
There are no bottles on the table
The variable is likened to a mineral water bottle, corresponding to 4 kinds of states:
General value: The storage unit contains a general value
Air value: variable is "", 0, "0", FALSE, Array () ...
Vacuum value: variable is null
Variable does not exist or is not defined
Pupils empty and middle school isset
Suppose we can use the isset empty function to determine if a mineral water bottle is empty, there will be such a result:
If it is an air bottle, then empty thinks it is empty (it does look empty!). And Isset thinks it's not empty (it has air!). )
If it's a vacuum bottle, then empty isset will think it's empty.
If it's a water bottle, then empty isset will think it's not empty.
can be seen, empty is a non-scientific pupils, thought the bottle looks empty is empty bottle! It's too young to be sure.
And Isset is a student who has studied physics and understands the concept of "vacuum" --he knows there's air in the bottle!
So the above three cases in the 1th, the old driver Isset experience, arrived with the pupils empty not the same conclusion.
Now we can see that if you use these two functions to determine the variables, the result would be:
If it is air value ("", 0, "0", FALSE, Array () ... ), then empty thinks it's empty, and Isset thinks it's not empty.
If it is a vacuum value (NULL), then empty isset thinks it is empty.
If it is a general value (except in both cases, i.e. there is water in the bottle), then empty isset thinks it is not empty.
This is the interview answer given at the beginning. Now, isn't it easy to understand?
Detailed comparison
Isset () function
Format: bool Isset (mixed var [, mixed Var [, ...])
Function: Detect whether the variable is set
return value:
Returns FALSE if the variable does not exist
Returns FALSE if the variable exists and its value is null
Returns TURE if the variable exists and the value is not NULL
Returns TRUE if each item meets the previous requirement when checking multiple variables at the same time, otherwise the result is FALSE
More information:
After you use Unset () to release a variable, it will no longer be isset ().
PHP functions Isset () can only be used for variables , and passing any other parameter will result in parsing errors.
Detects if a constant is set to use the defined () function.
Empty () function
Format: bool Empty (mixed Var)
function: Check if a variable is empty
return value:
returns TRUE If the variable does not exist
Returns TURE if the variable exists and its value is "", 0, "0", NULL, FALSE, Array (), Var $var, and an object without any attributes.
Returns FALSE if the variable exists and the value is not "", 0, "0", NULL, FALSE, Array (), Var $var, and an object without any attributes.
More information:
The return value of empty () =! (Boolean) Var, but does not produce a warning message because the variable is undefined. See Convert to Boolean for more information.
Empty () can only be used for variables , and passing any other parameter will cause Paser error and terminate the run.
Detects if a constant is set to use the defined () function.
The most complete difference
Now we put the mineral water and the variables together to draw the most complete Difference chart:
If you can read the previous content, you will thoroughly understand the two pictures, and then isset the empty difference is engraved in the mind. If someone asks you the difference, throw out the diagram right away.
Interested can read down, the Official Handbook gives examples ...
PHP Manual
This is a table that distinguishes the empty isset if ($var) from the table where we can find:
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!