From: Operating system: FREEBSD 8.0 PHP version: 5.2.13 Package: Arrays related Bug Type: Bug Bug description:foreach structure pass by reference scope issue
Description: ------------ When an ampersand operator is used in a foreach language construct The pass by reference variable causes *copies* of the parent array used in the foreach loop to be modified in any future use of the initial pass by reference variable. This is very hard to explain clearly but see the test script below for clarification it is a very simple test script.' Similar bugs like this have been reported and documentation outlines a simple workaround that does work and solves my problem. but I figured I would report this problem as *copies* of the array are affected and it seems kind of strange. Test script: --------------- class Monkey{ public $bananas=array(); public function AddBananas($bananas){ $this->bananas=$bananas; }} $bananas=array(); $bananas['banana1']=array('color'=>'yellow','size'=>'big'); $bananas['banana2']=array('color'=>'green','size'=>'small'); $coconuts=array(); $coconuts['coconut1']['size']='tiny'; $coconuts['coconut2']['size']="I'm a"; $monkey=new Monkey(); foreach($bananas as $key=>&$banana){ $banana['id']=$key+1; } $monkey->AddBananas($bananas); foreach($coconuts as $banana){ $banana['type']="coconut!"; } print_r($monkey->bananas); Expected result: ---------------- Array ( [banana1] => Array ( [color] => yellow [size] => big [id] => 1 ) [banana2] => Array ( [color] => green [size] => small [id] => 2 ) ) Actual result: -------------- Array ( [banana1] => Array ( [color] => yellow [size] => big [id] => 1 ) [banana2] => Array ( [size] => I'm a [type] => coconut! ) ) -- Edit bug report at http://bugs.php.net/bug.php?id=51409&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51409&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51409&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51409&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51409&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51409&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51409&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51409&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51409&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51409&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51409&r=support Expected behavior: http://bugs.php.net/fix.php?id=51409&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51409&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51409&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51409&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51409&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=51409&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51409&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51409&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51409&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51409&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51409&r=mysqlcfg