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 operators please help explain to me.
Moderator: General Moderators
-
nodnarb123
- Forum Newbie
- Posts: 4
- Joined: Tue Jul 19, 2011 3:18 pm
Re: reference operators please help explain to me.
Kinda tricky to explain but here goes.
Imagine you have two cups, cup A ($a) and cup B($b). If you add 5 coins to cup A and 5 coins to cup B each cup has 5 coins, if you counted the coins in either cup it would equal 5.
Then you add 2 more counts to cup A, cup A now has 7 coins and cup remains on 5. If you write my example out as PHP it would look like this.
Tough one to explain, but once you get it you will see how simple it is 
Imagine you have two cups, cup A ($a) and cup B($b). If you add 5 coins to cup A and 5 coins to cup B each cup has 5 coins, if you counted the coins in either cup it would equal 5.
Then you add 2 more counts to cup A, cup A now has 7 coins and cup remains on 5. If you write my example out as PHP it would look like this.
Code: Select all
$cupA = 5; //5 coins in cup A
$cupB = 5; //5 coins in cup B
$cupA = 7; //Now there are 7 coins in cup A, but B hasn't had any coins added to it so it remains on 5.
echo $cupA; //Would echo 7
echo $cupB; //Would echo 5
-
nodnarb123
- Forum Newbie
- Posts: 4
- Joined: Tue Jul 19, 2011 3:18 pm
Re: reference operators please help explain to me.
OH...... Okay I think I understand now. Thanks for helping me understand.