Fall into the trap in PHP-null, zero, false, empty string

Source: Internet
Author: User
Fall into the trap in PHP-null, 0, false, empty strings today, when I used the company framework for development and found that the data is updated, those empty strings are not updated to the database, that is to say, classes & #20284; at 0, empty strings, and null are not updated to the database after they appear: they are found during careful debugging and fall into the pitfalls in PHP-null, 0, false, empty string

When data is updated during development using the company's framework today, the null strings are not updated in the database, that is, they are similar to 0 and null strings, null is not updated to the database after it appears:

After careful debugging, we found that the empty () function is used:

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

Then, naturally, the condition is no longer met and the database is updated. what about this pitfall? Instead of empty (), use isset ():

        if(isset($params['config_id'])) {            $filters[] = array('config_id', '=', $params['config_id']);        }

Differences between the two functions:

Empty is used to check whether the variable is empty. isset is used to check whether the variable is set:

In your own words:

Empty is used to determine whether the content stored in this variable is 0, null, false, and an empty string.

Isset indicates whether the variable has been declared. it can be a string of 0, null, false, and null.

Empty has a large range and isset has a small range.


If the judgment is very accurate: '=' is recommended. in this case, php will check whether the types and values of the two variables are completely like.


Therefore, emtpy, isset, ===needs to be properly used in scenarios.



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.