#36070 [Opn]: foreach strange behaviour
| From: | dmtr79 at yahoo dot com | Date: | Mon, 04 Jun 2007 22:02:40 +0000 |
| Subject: | #36070 [Opn]: foreach strange behaviour | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-113367@lists.php.net to get a copy of this message | ||
ID: 36070
User updated by: dmtr79 at yahoo dot com
-Reported By: kdmtr at yahoo dot com
+Reported By: dmtr79 at yahoo dot com
Status: Open
Bug Type: Feature/Change Request
Operating System: Win32, Unix
PHP Version: 5.1.2
New Comment:
That's working fine on 5.2.3. Thanks.
Previous Comments:
------------------------------------------------------------------------
[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