c0dehunter
c0dehunter

Reputation: 6150

PHP reference no effect

I want to have a reference to an object in an array so I can change it later. In this case I am increasing the score of a particular user:

$guessing_user;
foreach($this->UUID_users[$UUID] as &$othr_user){
    if($othr_user['user_object'] == $user){
        $guessing_user=$othr_user;
        break;
    }
}
$guessing_user['score']+=10;

When I look in array at the original value ('score') it stays the same. I am probably missing something about references. Isn't the & sign in foreach enough?

Upvotes: 0

Views: 35

Answers (1)

Tim S.
Tim S.

Reputation: 13843

try

$guessing_user = &$othr_user;

I think PHP makes a copy when you set $guessing_user.

Upvotes: 2

Related Questions