PHP:只能通过引用传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17279695/
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:37:28  来源:igfitidea点击:

PHP: Only variables can be passed by reference

phppass-by-reference

提问by Edward Yu

I am getting this error on line 57: $password = str_replace($key, $value, $password, 1);

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

As far as I can tell, I am only passing in variables. Here is some more context:

据我所知,我只是传入变量。这里有一些更多的背景:

$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++;
    }

}

回答by immulatin

You can't pass a constant of 1, a fix is to set it to a variable as so.

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

Change:

改变:

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

to:

到:

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

UPDATE:Changed to declare variable outside of the method call from feedback in comments.

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

回答by newacct

Passing 1there makes no sense. (Why not pass 42, or -5?) The 4th parameter of str_replaceis onlyused to pass information back to you. The function does not use the original value of the variable at all. So what would be the point (even if allowed) of passing something in, if it is not used, and you are not going to use the new value sent back to you? That parameter is optional; just don't pass anything at all.

路过1没有任何意义。(为什么不通过42,或-5?)的第四个参数str_replace用于传递信息反馈给你。该功能不使用变量的原始价值可言。那么,如果不使用某些东西并且您不打算使用发回给您的新值,那么将其传入(即使允许)有什么意义呢?该参数是可选的;只是不要通过任何东西。