From:             marc at perkel dot com
Operating system: Linux
PHP version:      5.2.11
PHP Bug Type:     Scripting Engine problem
Bug description:  foreach needs to be fixed

Description:
------------
When using foreach and looping through an array the second time if the
index variable isn't unset the results are that the referenced variables is
used as the index rather than the named variable.

The issue can be solved if when the foreach is set up that it does an
unset on the variable passed as the "as" variable. PHP should be changed to
unset the parameter passed as the index into the array.


Reproduce code:
---------------
$myarray = array("one","two","three","four");

foreach ($myarray as &$x) {
   $x = "$x -";
   print "$x\n";
}

print "\n";

foreach ($myarray as $x) {
   print "$x\n";
}


Expected result:
----------------
one -
two -
three -
four -

one -
two -
three -
four -

Actual result:
--------------
one -
two -
three -
four -

one -
two -
three -
three -

-- 
Edit bug report at http://bugs.php.net/?id=50161&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=50161&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=50161&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=50161&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=50161&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=50161&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=50161&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=50161&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=50161&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=50161&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=50161&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=50161&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=50161&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=50161&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=50161&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=50161&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=50161&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=50161&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=50161&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=50161&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=50161&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=50161&r=mysqlcfg

Reply via email to