|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-12-21 09:44 UTC] lunepi at comlink dot org
try 1.:
$var[] = "1";
$var[] = "2";
foreach($var as $test)
{
echo $test;
}
reset($var);
echo current($var);
output -> 12
then try:
$var[] = "1";
$var[] = "2";
//reset($var);
foreach($var as $test)
{
echo $test;
current($var);
}
echo current($var);
output -> 121
this should be the output of the first script too?
Greetings lunepi
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 14:00:01 2025 UTC |
foreach essentially REMOVES the array pointer alltogether on the original array. $arr = array('a','b'); foreach ($arr as $v); var_dump( current($arr) ); $arr = array('a','b'); foreach ($arr as $v); reset($arr); var_dump( current($arr) ); Results: bool(false) string(1) "a" Using key() instead of current() results in NULL instead of false. Where did the pointer go? Conclusion: Either foreach() has a feature that removes the array pointer from the original array or it's a bug. If it's seen as a feature please explain why so it can be documented.