php.net |  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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
50 - 39 = ?
Subscribe to this entry?

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

Reproduce code:
---------------
<?php

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

foreach($a as $k => &$v)
{
	$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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] derick@php.net
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 http://www.php.net/support.php 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.

unset($v);
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 02:01:29 2024 UTC