#23195 [Bgs->Opn]: Use of $_POST/$_SESSION causes a endless loop

From: Date: Fri, 25 Apr 2003 05:10:48 +0000
Subject: #23195 [Bgs->Opn]: Use of $_POST/$_SESSION causes a endless loop
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-38346@lists.php.net to get a copy of this message
ID: 23195 User updated by: jc at mega-bucks dot co dot jp Reported By: jc at mega-bucks dot co dot jp -Status: Bogus +Status: Open Bug Type: Scripting Engine problem Operating System: Red Hat Linux 8.0 PHP Version: 4.3.2RC1 New Comment: Thanks for looking into this, but I cannot find anywhere in the documenation that states that assigning an array to a variable resets the array's pointer to the first element. Can you point me to such documentation? Furthermore if you are right, and I'm sure you are, then some of the documentation is wrong or confusing at best. Consider the following from the docs: http://www.php.net/manual/en/control-structures.foreach.php The following are also functionally identical: reset ($arr); while (list($key, $value) = each ($arr)) { echo "Key: $key; Value: $value<br>\n"; } foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br>\n"; } But has you said, those are *not* functionally identical. Or from http://www.php.net/manual/en/function.each.php each() is typically used in conjunction with list() to traverse an array; for instance, $_POST: Example 2. Traversing $_POST with each() echo "Values submitted via POST method:<br />\n"; reset ($_POST); while (list ($key, $val) = each ($_POST)) { echo "$key => $val<br />\n"; } Reading the documentation on each() and on arrays the above two examples teach that using a while(list() = each()) is a correct way of traversing an array. Which it obviously is *not* if you plan on assigning the array you are traversing to a variable ... Either the each() function should be removed to force the use of the assignment-safe 'foreach()' or the documentation should mention that when assigning an array to a variable the current element-pointer is reset. Previous Comments: ------------------------------------------------------------------------ [2003-04-24 19:49:05] iliaa@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php When you assign the array to another variable the internal array position pointer gets reset. Which causes you loop which depends on the array position to loop indefinately. Use foreach() or a for() loop. ------------------------------------------------------------------------ [2003-04-18 21:19:43] iliaa@php.net Here is a shorter example that demonstrates the problem: <?php $arr = array('a', 'b'); while (each($arr)) $arr2 = $arr; ?> ------------------------------------------------------------------------ [2003-04-14 01:15:09] jc at mega-bucks dot co dot jp The following piece of code never terminates: <?php while (list($k, $v) = each( $_POST )) { if(strstr($k, "CHANGE_DEL_DATES")) { session_set_post_vars( $_POST ); } } function session_set_post_vars($post) { $_SESSION["S_POST_VALUES"] = $post; } ?> The contents of the $_POST superglobal were: Array ( [CURRENT_DEL_METHOD] => YAMATO [DEL_YEAR1] => 2003 [DEL_MONTH1] => 04 [DEL_DAY1] => 12 [ptime1] => 1 [DEL_YEAR2] => 2003 [DEL_MONTH2] => 04 [DEL_DAY2] => 12 [DEL_YEAR3] => 2003 [DEL_MONTH3] => 04 [DEL_DAY3] => 12 [DELIVER_ANYDAY] => on [CHANGE_DEL_DATES_x] => 14 [CHANGE_DEL_DATES_y] => 7 ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=23195&edit=1

« previous php.bugs (#38346) next »