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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC