ID:               50223
 Updated by:       [email protected]
 Reported By:      disposable_address at kennel17 dot co dot uk
-Status:           Open
+Status:           Wont fix
 Bug Type:         Scripting Engine problem
 Operating System: Windows
 PHP Version:      5.2.11
 New Comment:

This is a known "problem":

php.net/foreach : "foreach  has some side effects on the array pointer.
Don't rely on the array pointer during or after the foreach without
resetting it." 


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

[2009-11-18 23:44:27] disposable_address at kennel17 dot co dot uk

Description:
------------
The array pointer does not behave in the expected manner within a
foreach() loop.  What's more, the behaviour differs between PHP4 and
PHP5.



Reproduce code:
---------------
$arr = array(1, 2, 3);

end($arr);

var_dump(current($arr));
foreach ($arr as $Item) {
print("Item " . $Item . " - ");
var_dump(current($arr));
}

var_dump(current($arr));


Expected result:
----------------
EITHER the array pointer IS NOT affected by the loop:

  int(3)
  Item 1 - int(3)
  Item 2 - int(3)
  Item 3 - int(3)
  int(3)

OR the array point IS affected by the loop:

  int(3)
  Item 1 - int(1)
  Item 2 - int(2)
  Item 3 - int(3)
  int(2)

I am not sure which is best, but it seems to me that any other
behaviour is incorrect.

Actual result:
--------------
In PHP4 (4.4.9):

  int(3)
  Item 1 - int(1)
  Item 2 - int(1)
  Item 3 - int(1)
  int(1)

In PHP5 (5.2.10)

  int(3)
  Item 1 - int(2)
  Item 2 - int(2)
  Item 3 - int(2)
  int(2)

Both of these seem wrong, but the fact that they are different seems
doubly wrong!  Calling reset() before the foreach() loop doesn't make
any difference.  The PHP4 version seems more acceptable (it sets it to
the first element on entry, and then doesn't affect it at all), however
the PHP5 behaviour just seems absurd.

Note: The described behaviour is the same on Windows Vista and Windows
XP.


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


-- 
Edit this bug report at http://bugs.php.net/?id=50223&edit=1

Reply via email to