3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $allowed_operators = array("*", "/", "^", "+", "-"); $rpn = "3 4 5 * -"; $rpnlen = strlen($rpn) - 1; $stack = array(); function digit($char) { return ctype_digit((string)$char); } //set_time_limit(2); function r_old() { global $pos, $rpn, $rpnlen, $stack, $allowed_operators, $result; if($rpnlen <= $pos) return false; $pos++; if($rpn[$pos] != ' ') { $tmp = ""; if(digit($rpn[$pos])) { while(digit($rpn[$pos]) || $rpn[$pos] == ".") { $tmp .= $rpn[$pos]; $pos++; } $pos--; if (!empty($tmp)) array_push($stack, $tmp); } else if(in_array($rpn[$pos], $allowed_operators)) { $second = array_pop($stack); $first = array_pop($stack); if($rpn[$pos] == '^') $result = eval("return pow($first, $second);"); else $result = eval("return ($first $rpn[$pos] $second);"); array_push($stack, $result); return r_old(); } else { return r_old(); } } return r_old(); } $result = 0; $pos = 0; function r() { global $pos, $rpn, $rpnlen, $allowed_operators, $result, $stack; while($rpn[$pos] == ' ') $pos++; if($rpnlen <= $pos) return false; if(digit($rpn[$pos])) { $tmp = ""; do { $tmp .= $rpn[$pos]; $pos++; } while(digit($rpn[$pos]) || $rpn[$pos] == "."); array_push($stack, $tmp); } else if(in_array($rpn[$pos], $allowed_operators)) { $second = array_pop($stack); $first = array_pop($stack); if($rpn[$pos] == '^') $result = eval("return pow($first, $second);"); else $result = eval("return ($first $rpn[$pos] $second);"); //echo "return ($first $rpn[$pos] $second);"; array_push($stack, $result); } $pos++; return r(); } r(); echo $result;
Output for 4.3.0 - 5.6.21, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
20