Home > Backend Development > PHP Tutorial > A picture to clarify the difference between isset and empty functions

A picture to clarify the difference between isset and empty functions

不言
Release: 2023-04-02 17:34:01
Original
3986 people have browsed it

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.

Let’s briefly talk about isset What is the difference between empty and empty?

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...

One picture to completely clarify

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

A picture to clarify the difference between isset and empty functions

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.

If you don’t understand something, or can’t remember all the details. Let me tell you how to remember everything about this picture!

Thoughts on Mineral Water

Imagine that there is a mineral water bottle on the table. The bottle may have the following 4 conditions:

  • 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

Analogize variables to mineral water bottles, corresponding to 4 states:

  • 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

Elementary school students empty and middle school students isset

Suppose we can use isset empty The function needs to determine whether a mineral water bottle is empty. The result will be as follows:

  • If it is an air bottle, then empty thinks it is empty (it does look empty) !), but isset thinks it is not empty (it has air!)

  • If it is a vacuum bottle, then empty isset will think it is empty

  • If it is a water bottle, then empty isset will think that it is not empty

It can be seen that empty is a primary school student who does not understand science, thinking that the bottle looks empty It's an empty bottle! Sure enough, he is still too young~
And isset is a middle school student who has studied physics and understands the concept of

"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.

Now we can know that if we use these two functions to judge variables, the result will be like this:

  • 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

This is the interview answer given at the beginning. Now, is it easy to understand?

Detailed comparison

isset() function

Format: bool isset (mixed var [, mixed var [, ...]])

Function : Check whether the variable is set

Return value:

If the variable does not exist, return FALSE
If the variable exists and its value is NULL, it also returns FALSE
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
More instructions:

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.

empty() function

Format: bool empty (mixed var)

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.

The most complete difference

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~

A picture to clarify the difference between isset and empty functions

A picture to clarify the difference between isset and empty functions

##If you are interested, you can read on, the examples given in the official manual...

PHP Manual

A picture to clarify the difference between isset and empty functions

This is a Zhang will show the difference between empty isset if($var). From the table we can find:

  • isset() return value is equivalent to !is_null()

  • The return value of empty() is equivalent to !boolean($x). if($x) actually converts the variable into a boolean type first, and then performs conditional judgment

    If $var does not exist, the if($x) statement will cause an E_NOTICE level exception in PHP, but it is not a fatal error. The script can still be executed;
    empty($x) and isset($x) will not report any exceptions

The above is the entire content of this article, I hope it will be helpful to everyone Learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template