php 不能在写上下文中使用函数返回值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17139264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:21:08  来源:igfitidea点击:

Can't use function return value in write context?

php

提问by Sophie Mackeral

I have this code:

我有这个代码:

public function __construct($Directory = null) 
{
    if ($Directory === null) {
        trigger_error("Directory Must Be Set!", E_USER_ERROR);
    }
    if (isset($Directory)) {
        if (!empty(trim($Directory))) { //Error Line
            echo "test"; 
        }
    }
}

(The echo is for my debugging purposes.)

(回声是为了我的调试目的。)

I get returned with the fatal error:

我返回致命错误:

Can't use function return value in write context

不能在写上下文中使用函数返回值

According to PHP storm this returns:

根据 PHP 风暴,这将返回:

Variable Expected

可变预期

But using the code directly from this question:

但是直接使用这个问题的代码:

White spaces throwing off HTML form validation

空格会影响 HTML 表单验证

This is the correct syntax, as i've used it in the past... But, in this situation this is throwing an error. Why is this?

这是正确的语法,因为我过去使用过它......但是,在这种情况下,这会引发错误。为什么是这样?

回答by Ken Herbert

From the PHP docs on the emptyfunction:

来自该empty函数的 PHP 文档:

Note: Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

注意:PHP 5.5 之前,empty() 只支持变量;其他任何事情都会导致解析错误。换句话说,以下将不起作用:empty(trim($name))。相反,使用trim($name) == false。

So you will have to split that line into something like the following:

因此,您必须将该行拆分为如下所示的内容:

$trimDir = trim($Directory);
if(!empty($trimDir))