Home > Article > Backend Development > Things to note when using empty() function in php_PHP Tutorial
When you use empty() to judge the number 0, empty() will also return true. That is to say, if your variable may have the number 0, it will be better to use isset() to judge! !
On the surface, it is easy to misunderstand that the empty() function is a function that determines whether a string is empty. In fact, it is not, and I suffered a lot because of it.
The empty() function is used to test whether the variable has been configured. If the variable already exists, is a non-empty string, or is non-zero, a false value is returned; otherwise, a true value is returned. Therefore, when the value of the string is 0, true is also returned, which is to execute the statement inside empty. This is the trap.
The code is as follows
|
Copy code
|
||||||||||
$a=array("1"=>"ddf"); To determine whether a string is empty, you can determine it like this: if ($value=="") ...
* Format: bool empty (mixed var) * If the variable does not exist, return TRUE * If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, then TURE
* Version: PHP 3, PHP 4, PHP 5 On the surface, it is easy to misunderstand that the empty() function is a function that determines whether a string is empty. In fact, it is not, and I suffered a lot because of it. The empty() function is used to test whether the variable has been configured. If the variable already exists, is a non-empty string, or is non-zero, a false value is returned; otherwise, a true value is returned. Therefore, when the value of the string is 0, true is also returned, which is to execute the statement inside empty. This is the trap. For example: Assume $value = 0; then empty($value)=false. I advise everyone to be careful about using the empty() function. To determine whether a string is empty, you can determine it like this: if ($value=="") ... Format: bool empty (mixed var) Function: Check whether a variable is empty Return value: If the variable does not exist, return TRUE If If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, TRUE is returned. If the variable exists and its value is not "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes, return FALSE Version: PHP 3, PHP 4, PHP 5 When you use empty to check the result returned by a function, an error will be reported: Fatal error: Can't use function return value in write contextFor example:
|
|||||||||||
echo empty(strlen('be-evil.org'));<🎜>
<🎜>
<🎜>
<🎜>
<🎜>Go to the PHP manual and see the following text where the empty function is described: <🎜>
<🎜>Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).<🎜>
<🎜>empty() only tests variables, testing anything that is not a variable will result in a parsing error!<🎜>
<🎜> Therefore, we cannot use empty to directly detect the value returned by the function. The solution to the above example is as follows: <🎜>
Statement: The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn Previous article:Registration and automatic loading of php classes__autoload_PHP tutorialNext article:Registration and automatic loading of php classes__autoload_PHP tutorial Related articlesSee more |