3v4l.org

run code in 300+ PHP versions simultaneously
<?php $iterator = new class implements \Iterator { private $values = [1, 2, 3]; private $key; public function current() { return current($this->values); } public function next() { next($this->values); } public function key() { return $this->key; } public function valid() { return current($this->values) !== false; } public function rewind() { echo "Rewinding\n"; reset($this->values); } }; $generator = (function () { yield 1; yield 2; yield 3; })(); $wrappedIterator = (function($iterator) { foreach ($iterator as $value) { yield $value; } })($iterator); function skipOne(\Iterator $iterator) { $iterator->next(); echo "Skipped one\n"; yield from $iterator; } echo "\nUsing skipOne(iterator):\n"; foreach (skipOne($iterator) as $value) { var_dump($value); } echo "\nUsing skipOne(generator):\n"; foreach (skipOne($generator) as $value) { var_dump($value); } echo "\nUsing skipOne(wrappedIterator):\n"; foreach(skipOne($wrappedIterator) as $value) { var_dump($value); } echo "\nUsing foreach(iterator):\n"; foreach($iterator as $value) { var_dump($value); }
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Deprecated: Return type of Iterator@anonymous::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/c2ode on line 8 Deprecated: Return type of Iterator@anonymous::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/c2ode on line 13 Deprecated: Return type of Iterator@anonymous::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/c2ode on line 18 Deprecated: Return type of Iterator@anonymous::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/c2ode on line 23 Deprecated: Return type of Iterator@anonymous::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/c2ode on line 28 Using skipOne(iterator): Skipped one Rewinding int(1) int(2) int(3) Using skipOne(generator): Skipped one int(2) int(3) Using skipOne(wrappedIterator): Rewinding Skipped one int(2) int(3) Using foreach(iterator): Rewinding int(1) int(2) int(3)
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Using skipOne(iterator): Skipped one Rewinding int(1) int(2) int(3) Using skipOne(generator): Skipped one int(2) int(3) Using skipOne(wrappedIterator): Rewinding Skipped one int(2) int(3) Using foreach(iterator): Rewinding int(1) int(2) int(3)
Output for 5.6.0 - 5.6.40
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/c2ode on line 2
Process exited with code 255.

preferences:
164.51 ms | 410 KiB | 5 Q