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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: msw at msw dot cz
New email:
PHP Version: OS:

 

 [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-2021 The PHP Group
All rights reserved.
Last updated: Sun Aug 01 14:01:24 2021 UTC