Bug #80955 [Opn->Nab]: References to elements in an array has side-affects to original array
| From: | imsop@php.net | Date: | Thu, 15 Apr 2021 13:12:17 +0000 |
| Subject: | Bug #80955 [Opn->Nab]: References to elements in an array has side-affects to original array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-233440@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80955&edit=1
ID: 80955
Updated by: imsop@php.net
Reported by: yahya12 at outlook dot com
Summary: References to elements in an array has side-affects
to original array
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Any
PHP Version: 8.0.3
Block user comment: N
Private report: N
New Comment:
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..."
Previous Comments:
------------------------------------------------------------------------
[2021-04-15 12:36:29] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80955&edit=1