Differences between empty and isset in php

Source: Internet
Author: User

Empty is used to determine whether the variable value is non-null or non-zero. Corresponding empty definitions include: "" (empty string), 0, "0", NULL, FALSE, array (), and $ var (declared only but not assigned values ). That is to say, when the value of the variable is the above, empty returns TRUE, and others return FALSE.

Isset checks whether the variable is set and is not NULL. Variable settings can be described in several aspects. 1: The simplest way is to declare and assign values to variables first; 2: Check whether the corresponding index or key exists in the array; 3: Check whether the corresponding attribute exists in the object.

From the two function definitions above, we can see that in some cases, the two can be shared, but the difference is still very large. In addition, they can only detect variables, and any non-variable detection will cause parsing errors. For example, if you directly check the return value of another function (empty (otherFunction (), you will see an error like "Fatal error: Can't use function return value in write context in.

In addition, isset can check multiple variables at a time, for example, isset ($ var1, $ var2, $ var3). When the isset values of these three values are TRUE, the result is TRUE, otherwise, the result is FALSE.


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, the two will return different values -- empty () think that there is no configuration, isset () can get the value of $ id:

 

The code is as follows: Copy code
$ Id = 0;
Empty ($ id )? Print "It's empty.": print "It's $ id .";
// Result: It's empty.
Print "<br> ";
! 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 shows 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, the id is also 1
If (! Isset ($ id) $ id = 1;-if id = 0, id is not 1


Run the following code separately to detect the above inference:

 

The code is as follows: Copy code

If (empty ($ id) $ id = 1;
Print $ id; // Get 1

If (! Isset ($ id) $ id = 1;
Print $ id; // get 0

Test 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 a value, 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, the variable declaration value is NULL

$ Var1 = ''; $ var2 = '0'; $ var3 = 0; $ var4 = FALSE; $ var5 = array ();

Echo $ sep. 'Test'' empty :';
Var_dump (empty ($ var1); // TRUE
Echo $ sep. 'Test'0' empty :';
Var_dump (empty ($ var2); // TRUE
Echo $ sep. 'Test 0 empty :';
Var_dump (empty ($ var3); // TRUE
Echo $ sep. 'Test FALSE empty :';
Var_dump (empty ($ var4); // TRUE
Echo $ sep. 'Test array () empty :';
Var_dump (empty ($ var5); // TRUE
Echo $ sep. 'Test', '0', 0, FALSE, array () isset :';
Var_dump (isset ($ var1, $ var2, $ var3, $ var4, $ var5); // TRUE:

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)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.