ID:               51049
 Updated by:       [email protected]
 Reported By:      timminn at gmail dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         Arrays related
 Operating System: Arch Linux 2.6.31
 PHP Version:      5.3.1
 New Comment:

Reusing a variable that you've used to hold value references in a 
foreach loop leads to Bad ThingsĀ™. That's why the manual page for 
foreach has a big red box recommending that you unset() the variable 
after you're done with the loop.

Recent bugs dealing with this include bug #50485, bug #50582 and bug 
#48561. This behaviour isn't going to be changed as it would break 
backward compatibility.

Closing.


Previous Comments:
------------------------------------------------------------------------

[2010-02-15 05:03:10] timminn at gmail dot com

the bug occurs ONLY WHEN:
MUST BE used in a STRAIGHT MANNER as ARRAY IN OBJECT, as shown in the
sbmitted code,

in other words, if we use
$sa  =  $o3->a;
and foreach( $sa as $k => & $v ){ ... }
the bug will not occur

------------------------------------------------------------------------

[2010-02-15 04:47:44] timminn at gmail dot com

Description:
------------
if AN ARRAY WITHIN AN OBJECT is used in "a straight manner" in the
foreach() loop,  with the references to its values[poor English, see the
SECOND foreach() in the submitted code to understand I mean],

the last value in the array will be REPACED with the second last one
ACCIDENTALLY, WITHOUT ANY ASSIGNMENT OPERATION.



Reproduce code:
---------------
<?php
    class myc{  public $a;  }
    $o3->a  =  Array(  11,  22  );

    //loop ONE
    foreach(  $o3->a  as  $k =>  $v  )
        echo $v. ' ';     //EXPECT 11 22 ,  CORRECT OUTPUT
    echo "\n=========\n";

    //loop TWO notice the reference & sign for $v
    foreach(  $o3->a  as  $k => & $v  )
        echo $v. ' ';    //EXPECT 11 22 ,  CORRECT OUTPUT
    echo "\n=========\n";

    //loop THREE
    foreach(  $o3->a  as  $k => $v  )
        echo $v. ' ';    //EXPECT 11 22 , BUT WRONG OUTPUT: 11 11
    echo "\n=========\n";
?>

Expected result:
----------------
11 22
11 22
11 22

Actual result:
--------------
11 22
11 22
11 11


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=51049&edit=1

Reply via email to