php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #23195 copying an array resets its pointer
Submitted: 2003-04-14 01:15 UTC Modified: 2003-06-22 19:13 UTC
From: jc at mega-bucks dot co dot jp Assigned: philip (profile)
Status: Closed Package: Documentation problem
PHP Version: 4.3.0 OS: all
Private report: No CVE-ID: None
 [2003-04-14 01:15 UTC] 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
)

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-18 21:19 UTC] iliaa@php.net
Here is a  shorter example that demonstrates the problem:
<?php
        $arr = array('a', 'b'); 
        while (each($arr)) $arr2 = $arr;
?>
 [2003-04-24 19:49 UTC] 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-25 00:10 UTC] 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-06-10 00:53 UTC] philip@php.net
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?
 [2003-06-10 01:58 UTC] sniper@php.net
This isn't a bug in anything but docs..

 [2003-06-10 02:05 UTC] jc at mega-bucks dot co dot jp
Fine, it's a lack of documentation about some weird PHP behaviour :) (yes it *is* weird)

Somebody please fix it. This bug keeps geeting closed and reopened and passed around. End the torment ^_~
 [2003-06-10 02:17 UTC] philip@php.net
pong
 [2003-06-21 22:14 UTC] philip@php.net
This behavior has been documented and will show up when the manual is next built, thanks for the report :)

http://cvs.php.net/cvs.php/phpdoc/en/reference/array/functions/each.xml

Also, added "explanation of pointers" to the TODO as currently no section in the manual clearly explains the topic of array pointers.
 [2003-06-22 19:13 UTC] jc at mega-bucks dot co dot jp
Thank you!

Can't wait to read the new section on array pointers. I had no idea that PHP's array were so sensitive to manipulation ;)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Aug 18 19:01:28 2024 UTC