#36070 [Opn->Csd]: foreach strange behaviour

From: Date: Mon, 04 Jun 2007 22:09:00 +0000
Subject: #36070 [Opn->Csd]: foreach strange behaviour
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-113368@lists.php.net to get a copy of this message
ID: 36070 Updated by: tony2001@php.net Reported By: dmtr79 at yahoo dot com -Status: Open +Status: Closed Bug Type: Feature/Change Request Operating System: Win32, Unix PHP Version: 5.1.2 Previous Comments: ------------------------------------------------------------------------ [2007-06-04 22:02:39] dmtr79 at yahoo dot com That's working fine on 5.2.3. Thanks. ------------------------------------------------------------------------ [2007-06-04 12:47:28] michael at chunkycow dot com dot au 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 ------------------------------------------------------------------------ [2006-01-18 19:00:07] dmtr79 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 (#113368) next »