3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function new_array(...$args) { return $args; } function fukin_eval($op, ...$args) { switch ($op) { case 'MUL': return fukin_eval(...multiply(...$args)); case 'DIV': return fukin_eval(...divide(...$args)); case 'INV': $params = $args[2] ?? []; return fukin_eval($args[0]->{$args[1]}(...$params)); default: return new_array($op, ...$args); } } function multiply(...$vals) { if (count($vals) === 0) { return ['RAT', 1, 1]; } $car = fukin_eval(...array_pop($vals)); if ($car[0] === 'ERR') { return $car; } if ($car[1] === 0) { ['RAT', 0, 1]; } $cdr = fukin_eval(...multiply(...$vals)); if ($cdr[0] === 'ERR') { return $car; } if ($cdr[1] === 0) { ['RAT', 0, 1]; } return ['RAT', $car[1] * $cdr[1], $car[2] * $cdr[2]]; } function divide(...$vals) { if (count($vals) === 0) { return ['RAT', 1, 1]; } $car = fukin_eval(...array_pop($vals)); if ($car[0] === 'ERR') { return $car; } if ($car[1] === 0) { return ['ERR', 'DIVISION BY ZERO']; } $cdr = fukin_eval(...divide(...$vals)); if ($cdr[0] === 'ERR') { return $car; } if ($cdr[2] === 0) { return ['ERR', 'DIVISION BY ZERO']; } return ['RAT', $car[2] * $cdr[1], $car[1] * $cdr[2]]; } var_dump(fukin_eval('MUL', ['RAT', 1, 1], ['RAT', 2, 1], ['RAT', 3, 1])); var_dump(fukin_eval('DIV', ['RAT', 1, 1], ['RAT', 0, 1])); var_dump(fukin_eval('MUL', ['RAT', 0, 1], ['DIV', ['RAT', 1, 1], ['RAT', 0, 1]])); var_dump(fukin_eval('MUL', ['DIV', ['RAT', 1, 1], ['RAT', 0, 1]], ['RAT', 0, 1])); class Foo implements Countable { function __construct($val) { $this->val = $val; } function count() { return $this->val; } } $a = new Foo(3); var_dump(fukin_eval('MUL', ['RAT', 2, 1], ['INV', $a, 'count']));
Output for 7.0.0 - 7.3.0alpha1
array(3) { [0]=> string(3) "RAT" [1]=> int(6) [2]=> int(1) } array(2) { [0]=> string(3) "ERR" [1]=> string(16) "DIVISION BY ZERO" } array(2) { [0]=> string(3) "ERR" [1]=> string(16) "DIVISION BY ZERO" } array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(1) } Notice: Undefined offset: 1 in /in/Ae3mI on line 32 Notice: Undefined offset: 1 in /in/Ae3mI on line 46 Notice: Undefined offset: 2 in /in/Ae3mI on line 46 array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(0) }
Output for hhvm-3.15.4
array(3) { [0]=> string(3) "RAT" [1]=> int(6) [2]=> int(1) } array(2) { [0]=> string(3) "ERR" [1]=> string(16) "DIVISION BY ZERO" } array(2) { [0]=> string(3) "ERR" [1]=> string(16) "DIVISION BY ZERO" } array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(1) } Notice: Undefined index: 1 in /in/Ae3mI on line 32 Notice: Undefined index: 1 in /in/Ae3mI on line 46 Notice: Undefined index: 2 in /in/Ae3mI on line 46 array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(0) }
Output for 5.6.0 - 5.6.30
Parse error: syntax error, unexpected '?' in /in/Ae3mI on line 14
Process exited with code 255.