ID:               23195
 Updated by:       [EMAIL PROTECTED]
-Summary:          Use of $_POST/$_SESSION causes a endless loop
 Reported By:      jc at mega-bucks dot co dot jp
 Status:           Open
-Bug Type:         Documentation problem
+Bug Type:         Arrays related
 Operating System: Red Hat Linux 8.0
 PHP Version:      4.3.2RC1
 New Comment:

This makes no sense to me, why would assigning an array to another
variable affect the original arrays pointer?  Even the array it's
assigned to keeps the original pointer:

<?php
$array = array('a','b','c');

print "1: " . current($array) . "\n"; // a
print "2: " . next($array)    . "\n"; // b

$array2 = $array;

print "3: " . current($array) . "\n"; // a
print "4: " . current($array2). "\n"; // b
?>

Either $array should also keep its pointer or $array2 should also have
a reset pointer.  I'd assume both would keep the current pointer, or
maybe just $array2 would have a reset pointer :). Current behavior is
certainly not documented, and are you guys sure this isn't a bug?


Previous Comments:
------------------------------------------------------------------------

[2003-04-25 00:10:48] jc at mega-bucks dot co dot jp

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.

------------------------------------------------------------------------

[2003-04-24 19:49:05] [EMAIL PROTECTED]

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] [EMAIL PROTECTED]

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

Reply via email to