|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-11-19 11:14 UTC] colder@php.net
[2009-11-20 11:39 UTC] disposable_address at kennel17 dot co dot uk
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 21 23:00:01 2025 UTC |
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.