php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37917 Original Array modified after copy
Submitted: 2006-06-26 12:52 UTC Modified: 2006-08-24 14:41 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: andrea dot busia at axis-sv dot it Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.1.4 OS: Linux / WinXp
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andrea dot busia at axis-sv dot it
New email:
PHP Version: OS:

 

 [2006-06-26 12:52 UTC] andrea dot busia at axis-sv dot it
Description:
------------
First problem: If I Create an array setting an element to a string and another element as a reference to the first, then copy the array into an other variable, if i modify the second variable also the original array is modified.

Second problem:
why the second code line ($a[0]="foo";) produces
  [0]=>
  &string(3) "foo"

instead of
  [0]=>
  string(3) "foo"

Thanks
Andrea Busia

Reproduce code:
---------------
<?
$a=array();
$a[0]="foo";
$a[1] =& $a[0];
var_dump($a);
$b=$a;
$b[0]="bar";
var_dump($a);
?>


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


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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-26 12:55 UTC] andrea dot busia at axis-sv dot it
Because for a mistake "Actual result" had to be "Expected result" and "Expected result" had to be "Actual result".
 [2006-08-24 14:41 UTC] tony2001@php.net
$a[1] =& $a[0];
This line means that both $a[0] and $a[1] become references that point to the same value. And when you do $b=$a;, $b recieves copies of these references, so the result you get is expected and correct.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 18 15:01:30 2024 UTC