3v4l.org

run code in 300+ PHP versions simultaneously
<?php // iterator impl: class MapIterator implements Iterator { private $f; private $inner; public function __construct($f, $inner) { $this->f = $f; $this->inner = $inner; } function rewind() { return $this->inner->rewind(); } function current() { return call_user_func($this->f, $this->inner->current()); } function key() { return $this->inner->key(); } function next() { return $this->inner->next(); } function valid() { return $this->inner->valid(); } } function iter_map($f, $iter) { return new MapIterator($f, $iter); } // generator impl: function gen_map($f, $iter) { foreach ($iter as $k => $v) { yield $k => $f($v); } } // example: function times_two($x) { return $x * 2; } $source = new ArrayIterator([0, 1, 2, 3, 4, 5, 6]); var_dump(iterator_to_array(iter_map('times_two', $source))); var_dump(iterator_to_array(gen_map('times_two', $source)));
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of MapIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/kUI8l on line 18 Deprecated: Return type of MapIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/kUI8l on line 26 Deprecated: Return type of MapIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/kUI8l on line 22 Deprecated: Return type of MapIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/kUI8l on line 30 Deprecated: Return type of MapIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/kUI8l on line 14 array(7) { [0]=> int(0) [1]=> int(2) [2]=> int(4) [3]=> int(6) [4]=> int(8) [5]=> int(10) [6]=> int(12) } array(7) { [0]=> int(0) [1]=> int(2) [2]=> int(4) [3]=> int(6) [4]=> int(8) [5]=> int(10) [6]=> int(12) }
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.30, 7.0.0 - 7.0.20, 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
array(7) { [0]=> int(0) [1]=> int(2) [2]=> int(4) [3]=> int(6) [4]=> int(8) [5]=> int(10) [6]=> int(12) } array(7) { [0]=> int(0) [1]=> int(2) [2]=> int(4) [3]=> int(6) [4]=> int(8) [5]=> int(10) [6]=> int(12) }
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$k' (T_VARIABLE) in /in/kUI8l on line 43
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/kUI8l on line 43
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_VARIABLE in /in/kUI8l on line 43
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/kUI8l on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/kUI8l on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/kUI8l on line 5
Process exited with code 255.

preferences:
310.69 ms | 401 KiB | 415 Q