【问题标题】:Can't use function return value in write context Laravel 4无法在写入上下文 Laravel 4 中使用函数返回值
【发布时间】:2015-01-31 15:44:34
【问题描述】:

我在代码中的第 430 行收到 Can't use function return value in write context 错误,但我不明白为什么会收到此错误..

奇怪的是,我只在服务器 (PHP 5.3) 上而不是在我的本地主机 (PHP 5.5.10) 上收到此错误

return  Redirect::route('account-activate-user', (empty(Input::get('code'))) ? '{code}' : e(Input::get('code')))
        ->with('global', 'De activatie-code is niet geldig.');

有没有人能解决这个问题?

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    这是因为您使用 empty() 和函数的返回值 (Input::get()),而它只接受一个变量。考虑到 Input::get() 的工作原理,即您可以在未设置输入时将第二个参数作为 默认 值传递,您可以完全跳过 empty() 检查并使用:

    return  Redirect::route('account-activate-user', Input::get('code', '{code}'))
            ->with('global', 'De activatie-code is niet geldig.');
    

    或者,更接近您的代码:

    return  Redirect::route('account-activate-user', (Input::has('code') ? '{code}' : e(Input::get('code')))
            ->with('global', 'De activatie-code is niet geldig.');
    

    【讨论】:

      【解决方案2】:

      在 PHP5.5 之前,函数 empty() 不能接受返回值。

      这意味着Input::get('code')返回一个值,这个值不能传递给empty()函数。

      虽然不是您可以通过这种方式快速解决的最佳解决方案:

      $inputCode = Input::get('code');
      
      return  Redirect::route('account-activate-user', (empty($inputCode)) ? '{code}' : e($inputCode))
      ->with('global', 'De activatie-code is niet geldig.');
      

      但是在这里您可以找到重复项:

      Can't use function return value in write context?

      【讨论】:

        【解决方案3】:

        我遇到了同样的错误,将 PHP 版本更新到 5.5.x 为我修复了它。

        【讨论】:

          猜你喜欢
          • 2016-09-02
          • 1970-01-01
          • 2017-11-01
          • 1970-01-01
          • 2012-06-20
          • 2011-04-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多