3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Everything you enter here will be executed by our servers. Try it! function iterator_pipeline(\Traversable $source, array $steps) { $gen = $source; foreach ($steps as $spec) { if (is_array($step) and !is_callable($spec)) { $g = array_shift($spec); array_unshift($spec, $gen); if (class_exists($g)) { $class = new \ReflectionClass($className); $gen = $class->newInstanceArgs($spec); } else if (is_callable($g)) { $gen = call_user_func_array($g, $spec); } } else if (is_callable($spec)) { $gen = $spec($gen); } } return $gen; } function iterator_chunk(\Traversable $it, $chunkSize) { $chunk = []; $steps = range(1, $chunkSize); while (true) { foreach ($steps as $_) { if (!$it->valid()) { break 2; } $chunk[] = $it->current(); $it->next(); } yield $chunk; $chunk = []; } if ($chunk) { yield $chunk; } } function iterator_consume(\Traversable $it) { foreach ($it as $key => $value) {} } function iterator_pass(\Traversable $it) { foreach ($it as $key => $value) { yield $key => $value; } } function iterator_tee(\Traversable $source, array $drains) { foreach ($source as $key => $value) { foreach ($drains as $gen) { $gen->send($value); } yield $key => $value; } } $receiver = function () { for (;;) { echo "TEE:\n"; print_r((yield)); } }; $receiver2 = function () { for (;;) { echo "Received!\n"; (yield); } }; $steps = [ function ($it) { foreach ($it as $val) { yield $val * 2; } }, "iterator_pass", ["iterator_chunk", 32], ["iterator_tee", [$receiver(), $receiver2()]] ]; $it = iterator_pipeline(new \ArrayIterator(range(0, 127)), $steps); iterator_consume($it);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: Undefined variable $step in /in/0nRqM on line 9 Warning: Undefined variable $step in /in/0nRqM on line 9 Warning: Undefined variable $step in /in/0nRqM on line 9 Warning: Undefined variable $step in /in/0nRqM on line 9
Output for 5.5.0 - 5.5.36, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.12 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: step in /in/0nRqM on line 9 Notice: Undefined variable: step in /in/0nRqM on line 9 Notice: Undefined variable: step in /in/0nRqM on line 9 Notice: Undefined variable: step in /in/0nRqM on line 9
Output for 7.3.32 - 7.3.33
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$chunk' (T_VARIABLE) in /in/0nRqM on line 42
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/0nRqM 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/0nRqM on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/0nRqM on line 14 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/0nRqM on line 27 Parse error: syntax error, unexpected '[' in /in/0nRqM on line 29
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/0nRqM on line 4 Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/0nRqM on line 4
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/0nRqM on line 4 Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/0nRqM on line 4
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
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/0nRqM on line 4 Parse error: parse error, unexpected T_STRING, expecting ')' in /in/0nRqM on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/0nRqM on line 4 Parse error: parse error, expecting `')'' in /in/0nRqM on line 4
Process exited with code 255.

preferences:
223.73 ms | 401 KiB | 346 Q