3v4l.org

run code in 300+ PHP versions simultaneously
<?php function chain($iterable, array $functions) { foreach ($functions as $function) { $iterable = $function(as_iterator($iterable)); } return $iterable; } function as_iterator($iterable) { if ($iterable instanceof \Iterator) { return $iterable; } if (is_array($iterable)) { return new \ArrayIterator($iterable); } if (is_string($iterable)) { return new \ArrayIterator(str_split($iterable)); } throw new \Exception(); } function do_map(callable $function) { return function(Iterator $iterator) use ($function) { foreach ($iterator as $key => $value) { yield $key => $function($value, $key); } }; } function do_filter(callable $function) { return function(Iterator $iterator) use($function) { foreach ($iterator as $key => $value) { if ($function($value, $key)) { yield $key => $value; } } }; } function do_concat($iterable) { return function(Iterator $iterator) use ($iterable) { foreach ($iterator as $value) { yield $value; } foreach (as_iterator($iterable) as $value) { yield $value; } }; } function do_as_array() { return function(Iterator $iterator) { return iterator_to_array($iterator); }; } $array = chain([1, 2, 3, 4], [ do_concat([5, 6, 7, 8]), do_filter(function ($v) { return $v % 2 == 0; }), do_map(function ($v) { return $v * 3; }), do_as_array(), ]); var_dump($array);
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 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, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
array(4) { [1]=> int(6) [3]=> int(12) [5]=> int(18) [7]=> int(24) }
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$key' (T_VARIABLE) in /in/fmS6q on line 29
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/fmS6q on line 29
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fmS6q on line 13 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fmS6q on line 17 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fmS6q on line 20 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fmS6q on line 22 Parse error: syntax error, unexpected T_FUNCTION in /in/fmS6q on line 27
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/fmS6q on line 3
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting '&' or T_VARIABLE or T_CONST in /in/fmS6q on line 3
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_ARRAY, expecting '&' or T_VARIABLE or T_CONST in /in/fmS6q on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'&'' or `T_VARIABLE' or `T_CONST' in /in/fmS6q on line 3
Process exited with code 255.

preferences:
301.53 ms | 401 KiB | 459 Q