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:25
Avg. Score:4.3 ± 1.0
Reproduced:20 of 20 (100.0%)
Same Version:4 (20.0%)
Same OS:5 (25.0%)
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jimmy at harlindong dot com
New email:
PHP Version: OS:

 

 [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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 17:01:30 2024 UTC