|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-10-22 17:29 UTC] 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 18:00:01 2025 UTC |
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.