Array assignment & references: strange behavior
| From: | Nenad Jovanovic | Date: | Thu, 30 Jun 2005 08:09:15 +0000 |
| Subject: | Array assignment & references: strange behavior | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-217873@lists.php.net to get a copy of this message | ||
Hi!
Under PHP 4.3.10, the following simple code behaves as expected: $b[1] and $c are not modified by the final assignment to $a[1]:
Code 1:
********
$a[1] = 1;
$b[1] = 2;
$c = 3;
$a = $b;
$a[1] = 7;
// resulting mappings:
// $a[1] ... 7
// $b[2] ... 2
// $c ...... 3
However, the following two codes result in changes to $b[1] and $c, the only difference to the previous code being the connection between $b[1] and $c via a reference:
Code 2:
********
$a[1] = 1;
$b[1] = 2;
$c =& $b[1];
$a = $b;
$a[1] = 7;
// resulting mappings:
// $a[1] ... 7
// $b[2] ... 7
// $c ...... 7
Code 3:
********
$a[1] = 1;
$b[1] =& $c;
$c = 2;
$a = $b;
$a[1] = 7;
// resulting mappings:
// $a[1] ... 7
// $b[2] ... 7
// $c ...... 7
Is this effect intended? If it is, is it specified somewhere in the PHP manual?
Thanks,
Nenad Jovanovic