foreach and current()
| From: | Nicolai Scheer | Date: | Mon, 18 Nov 2013 14:14:20 +0000 |
| Subject: | foreach and current() | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-70191@lists.php.net to get a copy of this message | ||
Hi all!
Has it been always the way, that it is not possible to use current() in a
foreach loop?
Just stumbled upon (using php 5.5.5):
$arr = array( "a", "b", "c" );
foreach( $arr as $entry )
{
#var_dump( current($arr) );
}
var_dump( current($arr) );
Which yields "false", because the internal array pointer points behind the
last element of $arr.
If I uncomment the inner var_dump(), the script outputs "b" four times.
This seems odd... I know that the documentation advises not to fiddle with
the internal array pointer in a foreach loop, but current() should not
affect it in any way and therefor should be safe to use.
Does anyone know what the reasoning behind this behavious is?
Greetings
Nico