php empty报错是因为empty只检测变量,检测任何非变量的东西都将导致解析错误,其解决办法就是不拿empty来直接检测函数返回的值,代码如“$length=strlen('test');echo empty($length);”。
推荐:《php视频教程》
php empty函数报错的解决办法
php empty函数在检测一个非变量情况下报错的解决办法。
php开发时,当你使用empty检查一个函数返回的结果时会报错:
fatal error: can't use function return value in write context
例如下面的代码:
<?php 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来直接检测函数返回的值,上面例子的解决方法如下:
<?php$length = strlen('test');echo empty($length);
以上就是php empty报错怎么办的详细内容。