3v4l.org

run code in 200+ PHP versions simultaneously
<?php function evalRPN($input) { $_GET['a']=$input; $v=$_GET['a']; while(preg_match('~(.*?)(-?[\d.]+) +(-?[\d.]+) +([*/+-])(.*)~',$v,$m)) $v=$m[1].($m[4]=="+"?$m[2]+$m[3]:($m[4]=="-"?$m[2]-$m[3]:($m[4]=="*"?$m[2]*$m[3]:$m[2]/$m[3]))).$m[5]; return $v; } function evalRPN2($input) { $_GET['a']=$input; $v=$_GET['a']; do{$v=preg_replace_callback( '~(-?[\d.]+) +(-?[\d.]+) +([*/+-])~', fn($m)=>($m[3]=="+"?$m[1]+$m[2]:($m[3]=="-"?$m[1]-$m[2]:($m[3]=="*"?$m[1]*$m[2]:$m[1]/$m[2]))), $v,9,$c );}while($c); return $v; } function evalRPN3($input) { $_GET['a']=$input; foreach(preg_split('~ +~', $_GET['a'])as$v) if(floatval($v))$s[]=$v;else{[$e,$d]=array_splice($s, -2);$s[]=$v=="+"?$e+$d:($v=="-"?$e-$d:($v=="*"?$e*$d:$e/$d));} return $s[0]; } echo evalRPN('-4 5 +') , "\n"; // output 1 echo evalRPN('5 2 /') , "\n"; // output 2.5 echo evalRPN('5 2.5 /') , "\n"; // output 2 echo evalRPN('5 1 2 + 4 * 3 - +') , "\n"; // output 14 echo evalRPN('4 2 5 * + 1 3 2 * + /') , "\n"; // output 2 echo evalRPN2('-4 5 +') , "\n"; // output 1 echo evalRPN2('5 2 /') , "\n"; // output 2.5 echo evalRPN2('5 2.5 /') , "\n"; // output 2 echo evalRPN2('5 1 2 + 4 * 3 - +') , "\n"; // output 14 echo evalRPN2('4 2 5 * + 1 3 2 * + /') , "\n"; // output 2 echo evalRPN3('-4 5 +') , "\n"; // output 1 echo evalRPN3('5 2 /') , "\n"; // output 2.5 echo evalRPN3('5 2.5 /') , "\n"; // output 2 echo evalRPN3('5 1 2 + 4 * 3 - +') , "\n"; // output 14 echo evalRPN3('4 2 5 * + 1 3 2 * + /') , "\n"; // output 2
based on tl3l9
Output for 7.4.0alpha1 - 7.4.0beta4
1 2.5 2 14 2 1 2.5 2 14 2 1 2.5 2 14 2
Output for 7.3.0 - 7.3.9
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /in/N81qc on line 14
Process exited with code 255.
Output for 7.1.25 - 7.2.22
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')' in /in/N81qc on line 14
Process exited with code 255.