[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-general
Subject:    [PHP] Array assignment & references: strange behavior
From:       Nenad Jovanovic <enji () gmx ! at>
Date:       2005-06-30 8:09:15
Message-ID: 42C3A8AB.6050409 () gmx ! at
[Download RAW message or body]

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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic