#36070 [Com]: foreach strange behaviour

From: Date: Mon, 04 Jun 2007 12:47:29 +0000
Subject: #36070 [Com]: foreach strange behaviour
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-113352@lists.php.net to get a copy of this message
ID: 36070 Comment by: michael at chunkycow dot com dot au Reported By: kdmtr at yahoo dot com Status: Open Bug Type: Feature/Change Request Operating System: Win32, Unix PHP Version: 5.1.2 New Comment: Howdy This works fine for me on 5.2.1, will test on 5.2.3 when the server is updated. output 1 1 1 2 2 1 2 2 --- 1 1 1 2 2 1 2 2 Previous Comments: ------------------------------------------------------------------------ [2006-01-18 19:00:07] kdmtr at yahoo dot com Description: ------------ foreach seems to run in different ways when called globally and from a function. As it can be understood from the documentation, foreach shouldn't touch an original array and operate with its copy instead. But at least, one could expect its behaviour to be similar for the 2 cases described in code. Reproduce code: --------------- $array = array(1, 2); function test() { global $array; foreach ($array as $a) foreach ($array as $b) echo "$a $b\n"; } foreach ($array as $a) foreach ($array as $b) echo "$a $b\n"; echo "---\n"; test(); Expected result: ---------------- 1 1 1 2 2 1 2 2 --- 1 1 1 2 2 1 2 2 Actual result: -------------- 1 1 1 2 2 1 2 2 --- 1 1 1 2 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=36070&edit=1

« previous php.bugs (#113352) next »