Edward Yu
Edward Yu

Reputation: 786

PHP: Only variables can be passed by reference

I am getting this error on line 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++;
    }

}

Upvotes: 18

Views: 44080

Answers (2)

newacct
newacct

Reputation: 122429

Passing 1 there makes no sense. (Why not pass 42, or -5?) The 4th parameter of str_replace is only used 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.

Upvotes: 11

immulatin
immulatin

Reputation: 2118

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

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.

Upvotes: 13

Related Questions