php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80955 References to elements in an array has side-affects to original array
Submitted: 2021-04-15 12:36 UTC Modified: 2021-04-15 13:12 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: yahya12 at outlook dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 8.0.3 OS: Any
Private report: No CVE-ID: None
 [2021-04-15 12:36 UTC] yahya12 at outlook dot com
Description:
------------
There is a bug in PHP where the below code returns an unexpected value.
It looks like making references to an array or elements has side-effects to the original array.

This is an issue that has been reported many times around 20 years ago, but reported as "won't fix" (I believe due to performance issues).
- https://bugs.php.net/bug.php?id=20993
- https://bugs.php.net/bug.php?id=6417
- https://bugs.php.net/bug.php?id=7412
- https://bugs.php.net/bug.php?id=15025

I'm unsure if this issue has been raised recently, but would like to see if this major bug will finally be fixed.

Also, can we at least have this documented in the official PHP docs. 
Currently I can only find this mentioned in comments:

- https://www.php.net/manual/en/language.references.php#79220
- https://www.php.net/manual/en/language.references.whatdo.php#102123
- https://www.php.net/manual/en/language.references.php#79220




Test script:
---------------
$a = [['name' => 'John']];
$unusedVariable = &$a[0]; // seems to modify $a[0], so its type &array
$b = $a;
$b[0]['name'] = "Bob";
echo $a[0]['name];

Expected result:
----------------
John

Actual result:
--------------
Bob

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-15 13:12 UTC] imsop@php.net
-Status: Open +Status: Not a bug
 [2021-04-15 13:12 UTC] imsop@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is documented on https://www.php.net/manual/en/language.references.whatdo.php

See the paragraph starting with "Note, however, that references inside arrays are potentially dangerous..."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC