Re: [PHP] strange reference behavior
Thanks for the clarifications. Regards, Robert Robert Cummings wrote: On Sat, 2007-09-01 at 11:39 +0100, Martin Ellingham wrote: Robert Enyedi wrote: Hi, I've been studying the PHP reference mechanism (with PHP 5.2.1) and I'm unsure if the following behavior is normal. This code works as expected: $a = 2; $b = &$a; //$c = &$a; $c = $b; $a = 1; echo $c."\n"; // Prints "2" as expected but this one does not: $a = 2; $b = &$a; $c = &$a; $c = $b; // Should overwrite the previous assignment, so $c // should get a copy of $b (and NOT a reference) $a = 1; echo $c."\n"; // I would expect "2", but prints "1" Could anyone please clarify why this happens? Regards, Robert This is because PHP5 has changed the default behaviour and $a = $b is now call by reference as standard. In the above example no objects have been used. As such, nothing has changed in the above semantics that do not exist in PHP4. Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] strange reference behavior
On Sat, 2007-09-01 at 11:39 +0100, Martin Ellingham wrote: > Robert Enyedi wrote: > > Hi, > > > > I've been studying the PHP reference mechanism (with PHP 5.2.1) and > > I'm unsure if the following behavior is normal. > > > > This code works as expected: > > > > $a = 2; > > $b = &$a; > > //$c = &$a; > > $c = $b; > > $a = 1; > > > > echo $c."\n"; // Prints "2" as expected > > > > but this one does not: > > > > $a = 2; > > $b = &$a; > > $c = &$a; > > $c = $b; // Should overwrite the previous assignment, so $c > > // should get a copy of $b (and NOT a reference) > > $a = 1; > > > > echo $c."\n"; // I would expect "2", but prints "1" > > > > Could anyone please clarify why this happens? > > > > Regards, > > Robert > > > This is because PHP5 has changed the default behaviour and $a = $b is > now call by reference as standard. In the above example no objects have been used. As such, nothing has changed in the above semantics that do not exist in PHP4. Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] strange reference behavior
On Sat, 2007-09-01 at 13:06 +0300, Robert Enyedi wrote: > Hi, > > I've been studying the PHP reference mechanism (with PHP 5.2.1) and I'm > unsure if the following behavior is normal. > > This code works as expected: > > $a = 2; > $b = &$a; > //$c = &$a; > $c = $b; > $a = 1; > > echo $c."\n"; // Prints "2" as expected > > but this one does not: > > $a = 2; > $b = &$a; > $c = &$a; > $c = $b; // Should overwrite the previous assignment, so $c >// should get a copy of $b (and NOT a reference) > $a = 1; > > echo $c."\n"; // I would expect "2", but prints "1" > > Could anyone please clarify why this happens? Sure... 1: $a = 2; 2: $b = &$a; 3: $c = &$a; 4: $c = $b; // Should overwrite the previous assignment, so $c 5:// should get a copy of $b (and NOT a reference) 6: $a = 1; 7: 8: echo $c."\n"; // I would expect "2", but prints "1" By line number... 1: Assign 2 to a variable called $a 2: Assign to $b a reference to $a 3: Assign to $c a reference to $a 4: Assign the value of $b to $c (this does NOT break $c's reference to $a) 6: Assign the value 1 to $a ($a is currently referenced by $b and $c) 8: Echo $c which should be 1. You will get the same result in PHP4 Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] strange reference behavior
Robert Enyedi wrote: Hi, I've been studying the PHP reference mechanism (with PHP 5.2.1) and I'm unsure if the following behavior is normal. This code works as expected: $a = 2; $b = &$a; //$c = &$a; $c = $b; $a = 1; echo $c."\n"; // Prints "2" as expected but this one does not: $a = 2; $b = &$a; $c = &$a; $c = $b; // Should overwrite the previous assignment, so $c // should get a copy of $b (and NOT a reference) $a = 1; echo $c."\n"; // I would expect "2", but prints "1" Could anyone please clarify why this happens? Regards, Robert This is because PHP5 has changed the default behaviour and $a = $b is now call by reference as standard. That's my understanding of it. Martin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] strange reference behavior
Hi, I've been studying the PHP reference mechanism (with PHP 5.2.1) and I'm unsure if the following behavior is normal. This code works as expected: $a = 2; $b = &$a; //$c = &$a; $c = $b; $a = 1; echo $c."\n"; // Prints "2" as expected but this one does not: $a = 2; $b = &$a; $c = &$a; $c = $b; // Should overwrite the previous assignment, so $c // should get a copy of $b (and NOT a reference) $a = 1; echo $c."\n"; // I would expect "2", but prints "1" Could anyone please clarify why this happens? Regards, Robert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php