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 7.0.0 - 7.1.0
array(10) { [0]=> float(0) [1]=> float(2) [2]=> float(4) [3]=> float(6) [4]=> float(8) [5]=> float(10) [6]=> float(12) [7]=> float(14) [8]=> float(16) [9]=> float(18) } Fatal error: Uncaught Exception: Cannot traverse an already closed generator in /in/3bPdm:39 Stack trace: #0 /in/3bPdm(74): reduce(Object(Closure), Object(Generator)) #1 {main} thrown in /in/3bPdm on line 39
Process exited with code 255.
Output for hhvm-3.12.0
array(10) { [0]=> float(0) [1]=> float(2) [2]=> float(4) [3]=> float(6) [4]=> float(8) [5]=> float(10) [6]=> float(12) [7]=> float(14) [8]=> float(16) [9]=> float(18) } NULL
Output for hhvm-3.10.0
array(10) { [0]=> float(0) [1]=> float(2) [2]=> float(4) [3]=> float(6) [4]=> float(8) [5]=> float(10) [6]=> float(12) [7]=> float(14) [8]=> float(16) [9]=> float(18) } Fatal error: Uncaught exception 'Exception' with message 'Generator is already finished' in /in/3bPdm:46 Stack trace: #0 /in/3bPdm(46): Generator->rewind() #1 /in/3bPdm(74): reduce() #2 {main}
Process exited with code 255.
Output for 5.5.24 - 5.6.28
array(10) { [0]=> float(0) [1]=> float(2) [2]=> float(4) [3]=> float(6) [4]=> float(8) [5]=> float(10) [6]=> float(12) [7]=> float(14) [8]=> float(16) [9]=> float(18) } Fatal error: Uncaught exception 'Exception' with message 'Cannot traverse an already closed generator' in /in/3bPdm:39 Stack trace: #0 /in/3bPdm(39): reduce() #1 /in/3bPdm(74): reduce(Object(Closure), Object(Generator)) #2 {main} thrown in /in/3bPdm on line 39
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$fn' (T_VARIABLE) in /in/3bPdm on line 7
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/3bPdm on line 7
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/3bPdm on line 5 Parse error: syntax error, unexpected T_VARIABLE in /in/3bPdm on line 7
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/3bPdm on line 5 Parse error: parse error, unexpected T_VARIABLE in /in/3bPdm on line 7
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/3bPdm on line 5
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/3bPdm on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/3bPdm on line 5
Process exited with code 255.