PHP empty函数报错解决办法_php技巧

WBOY
Release: 2016-05-17 08:48:11
Original
1070 people have browsed it

PHP开发时,当你使用empty检查一个函数返回的结果时会报错:Fatal error: Can't use function return value in write context

例如下面的代码:

复制代码 代码如下:

echo empty(strlen('test'));

转到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来直接检测函数返回的值,上面例子的解决方法如下:

复制代码 代码如下:

$length = strlen('test');
echo empty($length);
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!