Differences between isset and empty in php

Source: Internet
Author: User
What is the difference between isset and empty in php? Difference between isset and empty in php empty determines whether a variable is "null" and isset determines whether a variable has been set. It is this so-called "name suggests" that makes me take some detours at the beginning :??? 1. when a variable value is different from isset and empty in php

Http://seawomen.iteye.com/blog/629786

?

Difference between isset and empty in php empty determines whether a variable is "null" and isset determines whether a variable has been set. It is this so-called "name suggests" that makes me take some detours at the beginning :??? 1. when the value of a variable is equal to 0, empty () will also be True, which may lead to some accidents. Originally, although empty and isset are both variable processing functions, they are used to determine whether the variables have been configured, but there are some differences between them :??? 2. empty also checks whether the variable is null or zero. When the value of a variable is 0, empty considers this variable to be null, that is, it is equivalent to not being set. ?

??? 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. different values will be returned for both -- empty considers no configuration, isset can get the value of $ id:

Php code
  1. $ Id = 0 ;??
  2. EmptyEmpty($ Id )? Print? "It's? Empty ?. ": Print? "It's? $ Id ?. ";? ??
  3. // Result: It's? Empty ?. ??
  4. Print? "
    ";??
  5. ! Isset ($ id )? Print? "It's? Empty ?. ": Print? "It's? $ Id ?. ";??
  6. // Result: It's? 0 ?.??
$ Id = 0; empty ($ id )? Print "It's empty.": print "It's $ id."; // result: It's empty. print"
";! 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:

Php code
  1. If(EmptyEmpty($ Id ))? $ Id = 1 ;? // If? Id = 0 ?, Id? It will also be 1 ??
  2. If(! Isset ($ id ))? $ Id = 1 ;?? //? If? Id = 0 ?, Id? Not 1 ??
If (empty ($ id) $ id = 1; // if id = 0, the id is also 1if (! Isset ($ id) $ id = 1; // If id = 0, id will not be 1

?

Run the following code separately to detect the above inference:

Php code
  1. If(EmptyEmpty($ Id ))? $ Id = 1 ;??
  2. Print? $ Id ;? //? Get? 1 ??
  3. If(! Isset ($ id ))? $ Id = 1 ;??
  4. 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.