Req #70771 [Opn->Nab]: foreach($array['non_existant_key'] as &$row) silently modifies array
| From: | nikic@php.net | Date: | Thu, 22 Oct 2015 18:12:31 +0000 |
| Subject: | Req #70771 [Opn->Nab]: foreach($array['non_existant_key'] as &$row) silently modifies array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196755@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70771&edit=1
ID: 70771
Updated by: nikic@php.net
Reported by: stuckinabox at live dot com
Summary: foreach($array['non_existant_key'] as &$row)
silently modifies array
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: Arrays related
Operating System: all
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
A simpler case that illustrates the same issue:
$ref =& $data['non_existant_key'];
This will not throw a notice by design. You are allowed to take a reference to a key that does not
exist yet -- it will be initialized to NULL.
When you iterate by reference you are also implicitly creating a reference to the iterated entity.
Previous Comments:
------------------------------------------------------------------------
[2015-10-22 17:29:42] stuckinabox at live dot com
Description:
------------
Seems logical that an error should be thrown since that index doesnt exist in the array. instead,
the index is silently added with a value of null. All versions of php >5.4 and all versions of
HHVM are affected.
Repro: https://3v4l.org/UNIe4
Test script:
---------------
<?php
$data = [
'existant_key'=>[]
];
foreach($data['non_existant_key'] as &$row)
{
}
print_r($data);
?>
https://3v4l.org/UNIe4
Expected result:
----------------
undefined index
Actual result:
--------------
array is silently modified to include the non existant key
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70771&edit=1