Bugs php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login

go to bug id or search bugs for

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
[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

Pull Requests

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-2025 The PHP Group
All rights reserved. Last updated: Mon Dec 15 10:00:01 2025 UTC

AltStyle によって変換されたページ (->オリジナル) /