Talking about the difference between empty and isset in PHP

Source: Internet
Author: User

Empty is a value that determines whether the variable value is Non-null or Non-zero. Corresponding NULL definitions include: "" (empty string), 0, "0", NULL, FALSE, Array (), and $var (only declared but not assigned). That is, when the value of the variable is above, empty returns True and the rest returns false.

The isset is to detect whether the variable is set and is not NULL. Variable settings can be in several ways. 1: The simplest is whether the variable is declared and assigned first, and whether there is a corresponding attribute in index or key;3:object in the 2:array.

You can see from the two function definitions above that, in some cases, the two can be public, but the difference is great. In addition, they can only detect variables, and detecting anything that is not variable will result in parsing errors. For example, check the return value of another function directly (empty (Otherfunction ()), and you will see "Fatal error:can ' t use function returned value in write context" Such a mistake.

In addition, isset can check multiple variables at a time, such as: Isset ($var 1, $var 2, $var 3), and the result is false if each of the three values Isset is true.

Test code:

The code is as follows Copy Code

$sep = "<br/>";

Echo ' Test undeclared var empty: ';
Var_dump (Empty ($var)); TRUE
Echo $sep. ' Test undeclared var isset: ';
Var_dump (Isset ($var)); FALSE

$var;
Echo $sep. ' Test declared VAR but no set value empty: ';
Var_dump (Empty ($var)); TRUE
Echo $sep. ' Test declared VAR but no set value isset: ';
Var_dump (Isset ($var)); FALSE, the variable declaration is not assigned, and the default value is NULL

$var = NULL;
Echo $sep. ' Test declared Var and set value NULL empty: ';
Var_dump (Empty ($var)); TRUE
Echo $sep. ' Test declared Var and set value NULL isset: ';
Var_dump (Isset ($var)); FALSE, variable declaration assignment is null

$var 1 = '; $var 2 = ' 0 '; $var 3 = 0; $var 4 = FALSE; $var 5 = array ();

Echo $sep. ' Test ' empty: ';
Var_dump (Empty ($var 1)); TRUE
Echo $sep. ' Test ' 0 ' empty: ';
Var_dump (Empty ($var 2)); TRUE
Echo $sep. ' Test 0 empty: ';
Var_dump (Empty ($var 3)); TRUE
Echo $sep. ' Test FALSE empty: ';
Var_dump (Empty ($var 4)); TRUE
Echo $sep. ' Test Array () empty: ';
Var_dump (Empty ($var 5)); TRUE
Echo $sep. ' Test ', ' 0 ', 0, FALSE, Array () Isset: ';
Var_dump (Isset ($var 1, $var 2, $var 3, $var 4, $var 5)); TRUE, the variable affirms and assigns an empty string

The results of the output are:

  code is as follows copy code

Test Undeclared var Empty:bool (true)
Test undeclared var isset:bool (false)
Test declared Var but no set value empty : bool (true)
Test declared Var but no set value Isset:bool (false)
Test declared Var and set value NULL empty: BOOL (true)
Test declared Var and set value NULL Isset:bool (false)
Test ' Empty:bool (true)
Test ' 0 ' empty : bool (TRUE)
Test 0 Empty:bool (true)
Test FALSE Empty:bool (true)
Test array () Empty:bool (true)
Test ', ' 0 ', 0, FALSE, Array () Isset:bool (true)

For example, to detect $id variables, when $id = 0 o'clock, using empty () and isset () to detect whether the variable $id has been configured, both will return a different value--empty () do not think that the configuration, isset () can get $id value:

The code is as follows Copy Code

$id = 0;
Empty ($id) print "It's empty.":p rint "it's $id."
Result: It ' s empty.
print "<br>";
!isset ($id) print "It's empty.":p rint "it's $id."
Result: It ' s 0.

This means that when we use a variable-handling function, when the variable is likely to have a value of 0, use empty () to be careful, and it's wiser to replace it with Isset.

When the URL tail parameter of a PHP page appears id=0 (for example: test.php?id=0), try to compare:

The code is as follows Copy Code

if (empty ($id)) $id = 1; -If id=0, ID will also be 1
if (!isset ($id)) $id = 1; -if id=0, id not be 1

You can run the following code separately to detect the above inference:

  code is as follows copy code

 if ( Empty ($id)) $id = 1;
    print $id;//Get 1

    if (!isset ($id)) $id = 1;
    print $id;//Get 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.