【问题标题】:PHP: Only variables can be passed by referencePHP:只能通过引用传递变量
【发布时间】:2013-06-21 05:00:52
【问题描述】:

我在第 57 行收到此错误:$password = str_replace($key, $value, $password, 1);

据我所知,我只是传递变量。以下是更多上下文:

$replace_count = 0;
foreach($replacables as $key => $value)
{
    if($replace_count >= 2)
        break;
    if(strpos($password, $key) !== false)
    {
        $password = str_replace($key, $value, $password, 1);
        $replace_count++;
    }

}

【问题讨论】:

标签: php pass-by-reference


【解决方案1】:

在那里传递1 毫无意义。 (为什么不传递42-5?)str_replace 的第四个参数用于将信息返回给您。该函数完全不使用变量的原始值。那么,如果不使用某些东西,并且您不会使用发回给您的新值,那么传入某些东西(即使允许)又有什么意义呢?该参数是可选的;根本不通过任何东西。

【讨论】:

  • 没错,这是一个常见的错误,认为第四个参数是设置替换的数量。正如文档所说:count: If passed, this will be set to the number of replacements performed.
【解决方案2】:

您不能传递 1 的常量,解决方法是将其设置为变量。

变化:

$password = str_replace($key, $value, $password, 1);

到:

$var = 1
$password = str_replace($key, $value, $password, $var);

更新:更改为根据 cmets 中的反馈在方法调用之外声明变量。

【讨论】:

  • str_replace($key, $value, $password, $var = 1); 也不正确,使用str_replace($key, $value, $password, $var);。否则你会得到Strict Standards: Only variables should be passed by reference in......
  • @bystwn22 谢谢,我在本地没有这些设置,所以永远不会看到这个错误。我更新了答案
猜你喜欢
  • 1970-01-01
  • 2017-04-03
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多