4

Let's see this loop:

$tomb = [1, 2, 3];
foreach ($tomb as $x) {
    echo $x . '<br/>';
}
var_dump(current($tomb));

It displays:

1
2
3
bool(false)

It's correct, because the internal pointer advanced on every iteration, and on end it points over on array, current() returns false.

But this:

$tomb = [1, 2, 3];
foreach ($tomb as $x) {
    echo $x . ' : ' . current($tomb) . '<br/>';
}
var_dump(current($tomb));

Displays:

1 : 2
2 : 2
3 : 2
int(2)

Why? The internal pointer not advanced on every iteration, only once. The documentaion under current() says: "It does not move the pointer i n any way". Why the internal pointer only advanced by one, and not all of iterations? And why the current() affects the internal pointer?

Lay András
  • 795
  • 2
  • 9
  • 14

0 Answers0