Edit report at http://bugs.php.net/bug.php?id=54408&edit=1
ID: 54408 Updated by: ras...@php.net Reported by: hitchiker at mail dot ru Summary: Unexpected behaviour when using by-reference iteration -Status: Open +Status: Bogus Type: Bug Package: Arrays related Operating System: Win 7 x64, 2.6.18 PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: There is no expression-scope in PHP. If you create references in an array in a foreach loop or in any other expression, those references will still exist after the foreach. Follow the references and you will see that the output makes sense. There is no bug here. Previous Comments: ------------------------------------------------------------------------ [2011-04-26 08:33:18] blacknot at gmail dot com I have similar problem: $tags = array('iphone', 'apple', 'iPhone 4'); foreach ($tags as &$v) $v = trim($v); $tags2 = $tags; foreach ($tags2 as &$a) $a = '*'; print_r($tags); Result: Array ( [0] => iphone [1] => apple [2] => * ) ------------------------------------------------------------------------ [2011-04-08 13:10:27] hitchiker at mail dot ru I was sure that arrays are passed to functions by value, not by reference. It looks like by-reference iteration changes array not only isnide foreach statement, but array remains changed even after iteration. ------------------------------------------------------------------------ [2011-03-28 14:35:20] hitchiker at mail dot ru Description: ------------ I was sure that arrays are passed to functions by value, not by reference. It looks like after by-reference iteration changes array not only isnide foreach statement, but array remains changed even after iteration. Test script: --------------- <?php $a = array(1,2,3,4); function myf($a,$k){ $a[$k] = 9; } foreach ($a as $k => &$v) { myf($a, $k); } //myf($a, 3); var_dump($a); die(); Expected result: ---------------- array 0 => int 1 1 => int 2 2 => int 3 3 => int 4 Actual result: -------------- array 0 => int 9 1 => int 9 2 => int 9 3 => &int 9 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=54408&edit=1