3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // thanks to metaultralurker on reddit for inspiration function map(callable $fn, \Traversable $data) { foreach ($data as $v) { yield $fn($v); } } function filter(callable $fn, \Traversable $data) { foreach ($data as $v) { if ($fn($v)) { yield $v; } } } function infinite_range() { $i = 0; while (true) { yield $i++; } } function take($n, \Traversable $data) { $i = 0; foreach ($data as $v) { if ($i++ === $n) { break; } yield $v; } } function reduce(callable $fn, \Traversable $data, $runningTotal = null) { $first = true; foreach ($data as $v) { if (null === $runningTotal && $first) { $runningTotal = $v; continue; } $runningTotal = $fn($runningTotal, $v); $first = false; } return $runningTotal; } $isEven = function ($x) { return $x % 2 == 0; }; $data = take(10, map('floatval', filter($isEven, infinite_range()))); /* var_dump($data->current()); $data->next(); var_dump($data->current()); $data->next(); */ // var_dump(iterator_to_array($data)); $concat = function ($sep, $a, $b) { return $a.$sep.$b; }; $concatComma = function ($a, $b) use ($concat) { return $concat(',', $a, $b); }; var_dump(reduce($concatComma, $data));
Output for 5.5.24 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
string(24) "0,2,4,6,8,10,12,14,16,18"
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$fn' (T_VARIABLE) in /in/Pt82S on line 6
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/Pt82S on line 6
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Pt82S on line 4 Parse error: syntax error, unexpected T_VARIABLE in /in/Pt82S on line 6
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Pt82S on line 4 Parse error: parse error, unexpected T_VARIABLE in /in/Pt82S on line 6
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/Pt82S on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting ')' in /in/Pt82S on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/Pt82S on line 4
Process exited with code 255.