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 git.master, git.master_jit, rfc.property-hooks
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) }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
60.32 ms | 403 KiB | 8 Q