Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP remove "reference" from a variable.

I have the code below. I want to change $b to use it again with values. If I do so it changes $a as well. How can I assign a value to $b again after previously assigning it as a reference to $a?

$a = 1;
$b = &$a;

// later
$b = null;
like image 237
diolemo Avatar asked Jan 27 '12 09:01

diolemo


2 Answers

See explanation inline

$a = 1;    // Initialize it
$b = &$a;  // Now $b and $a becomes same variable with just 2 different names  
unset($b); // $b name is gone, vanished from the context  But $a is still available  
$b = 2;    // Now $b is just like a new variable with a new value. Starting new life.
like image 137
Shiplu Mokaddim Avatar answered Sep 28 '22 10:09

Shiplu Mokaddim


$a = 1;
$b = &$a;

unset($b);
// later
$b = null;
like image 44
xdazz Avatar answered Sep 28 '22 08:09

xdazz