首頁  >  文章  >  後端開發  >  php Note: empty() only checks variables as anything错误_PHP教程

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

WBOY
WBOY原創
2016-07-13 10:43:33913瀏覽

今天在利用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);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633171.htmlTechArticle今天在利用empty()函数判断一个变量是否为null是发现提示Note: empty() only checks variables as anything else will result in a parse error. In other words, the foll...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn