|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71421 Foreach with a reference is add content
Submitted: 2016-01-20 15:35 UTC Modified: 2016-01-20 18:31 UTC
From: msw at msw dot cz Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.0.2 OS:
Private report: No CVE-ID: None
 [2016-01-20 15:35 UTC] msw at msw dot cz
Run foreach with references on non-existent array index, add this index

Test script:
$a = array();
foreach($a['b'] as &$b){;}

Expected result:
Array ( ) 

Actual result:
Array ( [b] => )


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-20 18:31 UTC]
-Status: Open +Status: Not a bug
 [2016-01-20 18:31 UTC]
$a['b'] has to exist in order for $b to be a reference to something. If it does not exist then PHP will create it.
 [2016-01-21 08:31 UTC] msw at msw dot cz
Ok, I'll try use foreach more carefully. But I think it would be more logical to not change input variable. (If there is no imput ... no loop ... no reason create|use $b)
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Jul 03 07:03:35 2022 UTC