|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47388 Array Foreach Reference Broken
Submitted: 2009-02-14 19:47 UTC Modified: 2009-02-16 19:32 UTC
From: martin at itmission dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0beta1 OS: Windows XP
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.
Block user comment
Status: Assign to:
Bug Type:
From: martin at itmission dot com
New email:
PHP Version: OS:


 [2009-02-14 19:47 UTC] martin at itmission dot com
When using foreach on an array using the reference syntax, stuff breaks later.

Reproduce code:

$a = array(0, 1, 2, 3, 4);

foreach($a as $k => &$v)

foreach($a as $k => $v)
	echo("$k => $v\n");


Expected result:
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5

Actual result:
0 => 1
1 => 2
2 => 3
3 => 4
4 => 4


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-16 18:57 UTC] martin at itmission dot com
Also happens in 5.2.8
 [2009-02-16 19:15 UTC]
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

 [2009-02-16 19:32 UTC] martin at itmission dot com
The error is in my code.  Sorry.  Always unset variables after a foreach with a reference unless you know what you are doing.

 [2015-01-01 20:19 UTC] chealer at gmail dot com
The problematic behavior behind this issue is being tracked in #62132.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Jun 14 20:01:24 2021 UTC