Bug #70415 [Opn->Nab]: Reference &$array[] appends NULL element
| From: | nikic@php.net | Date: | Thu, 03 Sep 2015 06:21:29 +0000 |
| Subject: | Bug #70415 [Opn->Nab]: Reference &$array[] appends NULL element | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195729@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70415&edit=1
ID: 70415
Updated by: nikic@php.net
Reported by: vovan-ve at yandex dot ru
Summary: Reference &$array[] appends NULL element
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
PHP Version: 7.0.0RC1
Block user comment: N
Private report: N
New Comment:
References are note pure reads. Just like you can write =& $foo[2] even if index 2 does not yet
exist, you can also write $foo[]. Taking the reference on an undefined index will initialize the
index to NULL.
Previous Comments:
------------------------------------------------------------------------
[2015-09-03 03:41:10] vovan-ve at yandex dot ru
Description:
------------
Using $array[] for reading is permitted. But reference &$array[] erroneously is allowed and
appends NULL element silently.
Reproduced in all PHP versions up to latest 7.0.0rc1
https://3v4l.org/0l8OE
Test script:
---------------
function bar(&$var) {}
$foo = [10, 20];
$ref = &$foo[];
bar($foo[]);
var_dump($foo);
Expected result:
----------------
Fatal error: Cannot use [] for reading in ...
Actual result:
--------------
array(4) {
[0]=>
int(10)
[1]=>
int(20)
[2]=>
&NULL
[3]=>
NULL
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70415&edit=1