Home  >  Article  >  Backend Development  >  php Note: empty() only checks variables as anything error_PHP tutorial

php Note: empty() only checks variables as anything error_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:43:33915browse

Today I found a tip when using the empty() function to determine whether a variable is null. 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)). It was wrong, and it took me half a day to find the problem.


When you use empty to check the result returned by a function, the following fatal error will be reported:

Fatal error: Can't use function return value in write context in : .............

For example:

echo empty(yourfunction(xx, oo));

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. We need to assign the return value of the function to a variable first, and then use empty to detect the variable.

So, we can write it in the following form:

$return= yourfunction(xx, oo);

echo empty(return);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633171.htmlTechArticleToday I used the empty() function to determine whether a variable is null and found a prompt Note: empty() only checks variables as anything else will result in a parse error. In other words, the foll...
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