php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40385 inserting references to an array
Submitted: 2007-02-07 12:39 UTC Modified: 2007-02-07 12:45 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (100.0%)
From: viqq at gazeta dot pl Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.2.0 OS: Linux ubuntu 2.6.15-27-386
Private report: No CVE-ID: None
 [2007-02-07 12:39 UTC] viqq at gazeta dot pl
Description:
------------
When a reference to a variable (containing e.g. a literal or an object) is inserted into an array, all existing references in this array becomes references to this variable.


Reproduce code:
---------------
$t = NULL;

$node = '1';
// or $node = new Object();
$t[] = &$node;

$node = '2';
// or $node = new Object();
$t[] = &$node;

var_dump($t);


Expected result:
----------------
array(2) {
  [0]=>
  &string(1) "1"
  [1]=>
  &string(1) "2"
}

Actual result:
--------------
array(2) {
  [0]=>
  &string(1) "2"
  [1]=>
  &string(1) "2"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-07 12:45 UTC] tony2001@php.net
>$t[] = &$node
"$t[0] and $node both point to the same value".

>$node = '2';
"$node is changed to '2', $t[0] is changed either".

>$t[] = &$node;
"$t[1] points to the value already referenced by $node and $t[0]".
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 21 08:00:02 2025 UTC