Home  >  Article  >  php教程  >  php Note: empty() only checks variables as anything错误

php Note: empty() only checks variables as anything错误

WBOY
WBOYOriginal
2016-06-13 09:47:011224browse

今天在利用empty()函数判断一个变量是否为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)).错误了,后整一半天找到问题所在了。


你使用empty检查一个函数返回的结果时会报如下的一个致命错误:

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

例如:

echo empty(yourfunction(xx, oo));

到PHP手册里面查看,在empty函数描述的地方有以下文字:

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() 只检测变量,检测任何非变量的东西都将导致解析错误!

因此,我们不能拿empty来直接检测函数返回的值,需要先把函数的返回值赋给某个变量,然后去用empty检测这个变量。

所以,我们可以写成如下的形式:

$return= yourfunction(xx, oo);

echo empty(return);

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