php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15025 Copy of array is affected by reference
Submitted: 2002-01-13 22:18 UTC Modified: 2002-10-27 11:59 UTC
Votes:24
Avg. Score:4.3 ± 1.0
Reproduced:19 of 19 (100.0%)
Same Version:4 (21.1%)
Same OS:4 (21.1%)
From: jimmy at harlindong dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 4.3.0-dev OS: FreeBSD, Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2002-01-13 22:18 UTC] jimmy at harlindong dot com
You can guess / see from this simple script:

$a = array(5);
$b =& $a[0];
$c = $a;
echo $c[0];
$b = 1;
echo $c[0];

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-13 22:32 UTC] jimmy at harlindong dot com
$c should not have been affected by the assignment to $b because $c should only be a copy to $a.

What happened is the first echo shows 5 and after assignment to $b, the content of $c changes to 1 when it should stay as 5.
 [2002-09-17 13:31 UTC] kylam at superapple dot org
Verified with 4.2.3 on Windows 2000 too.
 [2002-09-17 20:50 UTC] iliaa@php.net
updating version.
 [2002-10-01 05:47 UTC] zeev@php.net
This has to do with the fact we don't deep-search when we copy values.  $a is not a reference, but has an element inside it which is a reference.  Therefore, when $a is copied to $c, and no deep search is made, $c ends up having that reference inside of it as well.
It may be possible to fix, but right now I'm just wanted to add the analysis here...
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC