PHP isset is different from empty

Source: Internet
Author: User
Isset and empty differences in PHP

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

?

The difference between PHP language isset and empty

Empty to determine whether a variable is "empty", isset to determine whether a variable has been set. It is this so-called "as the name implies", so I began to take some detours:??? 1. When a variable value equals 0 o'clock, empty () also holds (True), resulting in some unexpected occurrences. Originally, empty and isset are variable processing functions, they are used to determine whether the variables have been configured, they are a certain difference:

??? The 2.empty also detects if the variable is empty and zero. When the value of a variable is 0,empty that the variable equals null, it is equivalent to no setting.

?

??? For example, the detection of $id variables, when $id=0, with empty and isset to detect whether the variable $id has been configured, both will return different values ――empty think that there is no configuration, isset can obtain the value of $id:

PHP code

    1. $id=0;??
    2. empty empty ($id)? print? "It ' S?empty?" :p rint? "It ' S?$id?" ;???
    3. //result: It ' S?empty? ??
    4. print? " ;?
    5. !isset ($id)? print? "It ' S?empty?" :p rint? "It ' S?$id?" ;??
    6. //result: It ' s?0? ??

$id=0;empty ($id)? print "It's empty.":p rint "it ' s $id."; Result: It ' s empty. Print "
";! Isset ($id)? print "It's empty.":p rint "it ' s $id."; /Result: It ' s 0.

?

??? This means that when we use the variable handler function, when the variable may appear with a value of 0, use empty to be careful, it is wiser to replace it with Isset.

?

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

PHP code

    1. if (emptyempty($id))? $id=1;? //What if the Id=0?,id? will also be 1 . ??
    2. if (!isset ($id))? $id=1;?? //Id=0?,id? Not for 1 . ??

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

?

The following code can be run separately to detect the above inference:

PHP code

    1. if (emptyempty($id))? $id=1;??
    2. print?$id;? //? 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.