Differences between PHP5.5 and previous versions of the empty function

  • 2021-06-29 10:42:08
  • OfStack

As one of my favorite functions, I finally found the devil of this function today.Yang Yang spilled the following code, local test 1 ok, SB on the server.


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

Roughly speaking, a password must be greater than 6 bits and cannot consist of only numbers.Looking through the server logs, the following errors were found:

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

google1, roughly speaking, the parameters of empty cannot be functions.I depend on it. It's clear that the place is good.Look at the local PHP is 5.5 and the server is 5.3.Did this function evolve between the two versions?Searching through the official documents of PHP, no clues were found, then google searched wildly and accidentally hit the English document interface of PHP. Under the introduction of the empty function, a line of small words was found:


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.

Devil, you don't have such a pit for Chinese users.Summarize the usage scenarios for the empty function:

Prior to version 1.PHP 5.5, this function is used to check whether a variable is assigned a value of 0, false, empty string, null.Any argument in the form of a nonvariable causes this function to fail.
2.PHP 5.5 This function can be applied to any value, not just variables.You can return values for constants, functions, and so on.


Related articles: