Use the empty function of PHP with caution

Source: Internet
Author: User
As one of my favorite functions, today I have finally discovered the devil of this function. I wrote the following code freely. I tested everything locally and went to the service as B. If (strlen ($ passwd) 6 | empty ($ preg_replace (d, $ passwd) {dosomething} roughly means that the password must be greater than 6 digits cut

As one of my favorite functions, today I have finally discovered the devil of this function. I wrote the following code freely. I tested everything locally and went to the service as B. If (strlen ($ passwd) 6 | empty ($ preg_replace ("/\ d/", "", $ passwd ))) {// do something} indicates that the password must be greater than six digits.

As one of my favorite functions, today I have finally discovered the devil of this function. I wrote the following code freely. I tested everything locally and went to the service as B.

if(strlen($passwd) < 6 || empty($preg_replace("/\d/", "", $passwd))) {    //do something}

The password must be greater than six digits and cannot be composed of only digits. After searching server logs, the following error is found:

PHP Fatal error: Can't use function return value in write context in/xxx/xx. php on line xxx

Google: probably, the empty parameter cannot be a function. I rely on it. It is good to be local. I can see that the local PHP is 5.5, and the server is 5.3. Does this function evolve between two versions? I searched for official PHP documents and found no clues. Then I searched google and accidentally clicked on the PHP English document interface. Below the introduction of the empty function, I found a line of small words:

Note:
Prior to PHP 5.5, empty () only supports variables; anything else will result in a parse error. in other words, the following will not work: empty (trim ($ name )). instead, use trim ($ name) = false.

The devil, not Chinese users. To sum up the use cases of empty functions:

  1. In versions earlier than PHP 5.5, this function is used to check whether the value of the variable is 0, false, empty string, or null. Any parameter in non-variable form causes this function to report an error.
  2. PHP 5.5 can be applied to any value, not limited to variables. Can be constants, function return values, and so on.

2 links are attached to see how the document is intended:

PHP: empty Chinese Reference

PHP: empty

Original article address: Be careful when using the empty function of PHP. Thank you for sharing it with the original author.

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.