3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Reader extends IteratorIterator { private $is_first = true; public function current() { $this->is_first = false; return parent::current(); } public function valid() { $res = parent::valid(); if (!$res) $this->is_first = true; } public function rewind() { if (!$this->is_first) return; $this->is_first = true; parent::rewind(); } } $a = new Reader(new ArrayIterator(range(1, 10))); foreach ($a as $v) { echo $v . PHP_EOL; break; } foreach ($a as $v) { echo $v . PHP_EOL; break; } foreach ($a as $v) { echo $v . PHP_EOL; break; }

preferences:
38.37 ms | 402 KiB | 5 Q