php.net |  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
Description:
------------
Run foreach with references on non-existent array index, add this index

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

Expected result:
----------------
Array ( ) 

Actual result:
--------------
Array ( [b] => )

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-20 18:31 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-01-20 18:31 UTC] requinix@php.net
$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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC