PHP null value judgment

Source: Internet
Author: User

When using PHP to write page programs, I often use the variable processing function to determine whether the variable value of a parameter at the end of the PHP page is null. At the beginning, I used to use the empty () function, but some problems are found. Therefore, the isset () function is used instead.

As the name suggests, empty determines whether a variable is "null" and isset determines whether a variable has been set. This so-called "name implies" makes me take some detours at the beginning: When a variable value is equal to 0, empty () will also be true ), as a result, some accidents may occur. Originally, although empty and isset are both variable processing functions, they are used to determine whether the variables have been configured, but they are different: Empty also checks whether the variables are empty and 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:

$ 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 appears (for example, test. php? Id = 0), try to compare:

If (empty ($ id) $ id = 1;-if the ID is 1
If (! Isset ($ id) $ id = 1;-if the ID is not 1

Run the following code separately to detect the above inference:

If (empty ($ id) $ id = 1;
Print $ ID; // get 1

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

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/akailee/archive/2009/04/23/4104283.aspx

Related Article

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.