3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ExpressionCalculator { private $operators = array(); public function __construct() { $operators['+'] = function ($leftOperand, $rightOperand) { return $leftOperand + $rightOperand; }; $operators['-'] = function ($leftOperand, $rightOperand) { return $leftOperand - $rightOperand; }; $operators['*'] = function ($leftOperand, $rightOperand) { return $leftOperand * $rightOperand; }; $operators['/'] = function ($leftOperand, $rightOperand) { return $leftOperand / $rightOperand; }; } public function calc($expression) { $operands = array(); while (($token = strtok($expression, ' ')) !== false) { if (isOperator($token)) { $rightOperand = array_pop($operands); $leftOperand = array_pop($operands); $result = $this->execute($token, $leftOperand, $rightOperand); array_push($operands, $result); } else if (is_numeric($token)) { array_push($operands, $token); } else { throw new InvalidArgumentException('Expression has invalid token: ' + $token); } } return $result; } private function isOperator($token) { return isset($operators[$token]); } private function execute($operator, $leftOperand, $rightOperand) { return call_user_func($operators[$operator], $leftOperand, $rightOperand); } } $calc = new ExpressionCalculator(); echo $calc->calc('5 1 2 + 4 * + 3 -'); // 14
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught Error: Call to undefined function isOperator() in /in/G3eam:25 Stack trace: #0 /in/G3eam(50): ExpressionCalculator->calc('5 1 2 + 4 * + 3...') #1 {main} thrown in /in/G3eam on line 25
Process exited with code 255.

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:
33.48 ms | 401 KiB | 8 Q