3v4l.org

run code in 300+ PHP versions simultaneously
<?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 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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) } Deprecated: Return type of Foo::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Ae3mI on line 92 Deprecated: Creation of dynamic property Foo::$val is deprecated in /in/Ae3mI on line 90 Warning: Undefined array key 1 in /in/Ae3mI on line 32 Warning: Undefined array key 1 in /in/Ae3mI on line 46 Warning: Undefined array key 2 in /in/Ae3mI on line 46 array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(0) }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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) } Deprecated: Return type of Foo::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Ae3mI on line 92 Deprecated: Creation of dynamic property Foo::$val is deprecated in /in/Ae3mI on line 90 Warning: Undefined array key 1 in /in/Ae3mI on line 32 Warning: Undefined array key 1 in /in/Ae3mI on line 46 Warning: Undefined array key 2 in /in/Ae3mI on line 46 array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(0) }
Output for 8.1.0 - 8.1.28
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) } Deprecated: Return type of Foo::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Ae3mI on line 92 Warning: Undefined array key 1 in /in/Ae3mI on line 32 Warning: Undefined array key 1 in /in/Ae3mI on line 46 Warning: Undefined array key 2 in /in/Ae3mI on line 46 array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(0) }
Output for 8.0.0 - 8.0.30
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) } Warning: Undefined array key 1 in /in/Ae3mI on line 32 Warning: Undefined array key 1 in /in/Ae3mI on line 46 Warning: Undefined array key 2 in /in/Ae3mI on line 46 array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(0) }
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
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 7.3.32 - 7.3.33, 7.4.26
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) } array(3) { [0]=> string(3) "RAT" [1]=> int(0) [2]=> int(0) }
Output for 5.6.0 - 5.6.40
Parse error: syntax error, unexpected '?' in /in/Ae3mI on line 14
Process exited with code 255.

preferences:
241.75 ms | 401 KiB | 291 Q