Fall into the hole in PHP-null, 0, False, empty string

Source: Internet
Author: User
Fall into the hole in PHP--null,0,false, empty string

When the update data was discovered today using the framework of the company, those empty strings were not updated in the database, that is, like 0, an empty string, and null these occurrences were not updated in the database:

When carefully debugged, I found that the function of empty () was used:

        if (!empty ($params [' ID '])) {            $filters [] = array (' id ', ' = ', $params [' id ']);        }

Then naturally there is no such condition, to update the database, the pit how to fill it? Just do not use empty (), use Isset ():

        if (Isset ($params [' config_id '])) {            $filters [] = Array (' config_id ', ' = ', $params [' config_id ']);        }

The difference between the two functions:

Look at the name of the light can be seen empty is to see if this variable is null, isset see if this variable is set:

In your own words:

Empty is to judge what is stored inside this variable is 0,null,false, an empty string.

Isset is whether this variable has been declared, it can be a 0,null,false, an empty string.

Empty has a larger range and a smaller range of isset.


If you want to be very precise: it is recommended to use ' = = = ', so that PHP will detect whether the type and value of the two variables are exactly like.


So emtpy,isset,=== need to see the use of the scene reasonable use.



  • 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.