php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29687 By-reference passed value inside foreach() is no longer working
Submitted: 2004-08-15 15:19 UTC Modified: 2009-06-12 01:56 UTC
Votes:8
Avg. Score:4.9 ± 0.3
Reproduced:8 of 8 (100.0%)
Same Version:4 (50.0%)
Same OS:2 (25.0%)
From: jakub dot phpbug at horky dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 6CVS-2006-04-28 (snap) OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jakub dot phpbug at horky dot net
New email:
PHP Version: OS:

 

 [2004-08-15 15:19 UTC] jakub dot phpbug at horky dot net
Description:
------------
A value passed inside foreach() costruct by reference does not reflect the changes made to that value when the iterated array is touched by some array function, like key() or current().

In example below, if you comment out the key() line, it will work OK.

I think that this bug is closely related to the bug #28939.


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

$array = array("NO", "NO", "NO");

foreach($array as $index => &$item)
{
  key($array); // there may be also current(), reset(),
               // first(), next(), prev(), foreach etc.
  $item = "YES";
  var_dump($item);
}

print_r($array);

?>

Expected result:
----------------
string(3) "YES"
string(3) "YES"
string(3) "YES"
Array
(
    [0] => YES
    [1] => YES
    [2] => YES
)


Actual result:
--------------
string(3) "YES"
string(3) "YES"
string(3) "YES"
Array
(
    [0] => YES
    [1] => NO
    [2] => NO
)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-19 02:00 UTC] sniper@php.net
See bug #28939

 [2006-04-28 19:10 UTC] jakub dot phpbug at horky dot net
Please review the bug. It ISN'T bogus indeed, although it appears so. The code snippet provided proves that the behavior is really incorrect. Admit that when you only "touch" the array (by read-only function such as key()), you lose your reference-passing? More, you don't lose it in current cycle but in the NEXT cycle and subsequent? Hmm...

I tested it on today's snap and still the same behavior.

I'm really looking forward to the moment when I can remove the workaround from all reference-passed foreachs from all of my projects... thanks.
 [2006-04-28 19:21 UTC] jakub dot phpbug at horky dot net
(Also note that unlike bug #28939, there are _referenced_ arrays in question.)
 [2007-08-04 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2009-06-12 01:56 UTC] jakub dot phpbug at horky dot net
Yes, the problem appears to be solved in the new version. Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 18 03:01:27 2024 UTC