reference operators please help explain to me.
Posted: Tue Jul 19, 2011 3:25 pm
I'm reading a php book and it says here below that $b will not change if $a does. This don't make since to me. Will someone please explain? Seems to me if I where to echo $b it would equal 5 or 7 or 9 or whatever I decide to change $a to. Will you please explain?
Reference Operator
The reference operator (&, an ampersand) can be used in conjunction with assignment. Normally, when one variable is assigned to another, a copy is made of the first variable and stored elsewhere in memory. For example,
$a = 5; $b = $a;
These code lines make a second copy of the value in $a and store it in $b. If you subsequently change the value of $a, $b will not change:
$a = 7; // $b will still be 5
Reference Operator
The reference operator (&, an ampersand) can be used in conjunction with assignment. Normally, when one variable is assigned to another, a copy is made of the first variable and stored elsewhere in memory. For example,
$a = 5; $b = $a;
These code lines make a second copy of the value in $a and store it in $b. If you subsequently change the value of $a, $b will not change:
$a = 7; // $b will still be 5