不能在写上下文中使用函数返回值? [英] Can't use function return value in write context?

查看:91
本文介绍了不能在写上下文中使用函数返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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.)

我返回了致命错误:

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

Can't use function return value in write context

根据PHP风暴,此返回:

According to PHP storm this returns:

期望变量

但是直接使用此问题中的代码:

But using the code directly from this question:

空白导致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?

推荐答案

从PHP文档中的empty函数:

From the PHP docs on the empty function:

注意: 在PHP 5.5之前,empty()仅支持变量.其他任何情况都会导致解析错误.换句话说,以下内容将不起作用:empty(trim($ name)).而是使用trim($ name)== false.

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.

因此,您必须将该行分成以下内容:

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

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

这篇关于不能在写上下文中使用函数返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆