3v4l.org

run code in 300+ 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
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N81qc
function name:  (null)
number of ops:  76
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   INIT_FCALL                                               'evalrpn'
          1        SEND_VAL                                                 '-4+5+%2B'
          2        DO_FCALL                                      0  $0      
          3        ECHO                                                     $0
          4        ECHO                                                     '%0A'
   27     5        INIT_FCALL                                               'evalrpn'
          6        SEND_VAL                                                 '5+++2+%2F'
          7        DO_FCALL                                      0  $1      
          8        ECHO                                                     $1
          9        ECHO                                                     '%0A'
   28    10        INIT_FCALL                                               'evalrpn'
         11        SEND_VAL                                                 '5+2.5++++%2F'
         12        DO_FCALL                                      0  $2      
         13        ECHO                                                     $2
         14        ECHO                                                     '%0A'
   29    15        INIT_FCALL                                               'evalrpn'
         16        SEND_VAL                                                 '5+1+2+%2B+4+%2A+3+-+%2B'
         17        DO_FCALL                                      0  $3      
         18        ECHO                                                     $3
         19        ECHO                                                     '%0A'
   30    20        INIT_FCALL                                               'evalrpn'
         21        SEND_VAL                                                 '4+2+5+%2A+%2B+1+3+2+%2A+%2B+%2F'
         22        DO_FCALL                                      0  $4      
         23        ECHO                                                     $4
         24        ECHO                                                     '%0A'
   31    25        INIT_FCALL                                               'evalrpn2'
         26        SEND_VAL                                                 '-4+5+%2B'
         27        DO_FCALL                                      0  $5      
         28        ECHO                                                     $5
         29        ECHO                                                     '%0A'
   32    30        INIT_FCALL                                               'evalrpn2'
         31        SEND_VAL                                                 '5+++2+%2F'
         32        DO_FCALL                                      0  $6      
         33        ECHO                                                     $6
         34        ECHO                                                     '%0A'
   33    35        INIT_FCALL                                               'evalrpn2'
         36        SEND_VAL                                                 '5+2.5++++%2F'
         37        DO_FCALL                                      0  $7      
         38        ECHO                                                     $7
         39        ECHO                                                     '%0A'
   34    40        INIT_FCALL                                               'evalrpn2'
         41        SEND_VAL                                                 '5+1+2+%2B+4+%2A+3+-+%2B'
         42        DO_FCALL                                      0  $8      
         43        ECHO                                                     $8
         44        ECHO                                                     '%0A'
   35    45        INIT_FCALL                                               'evalrpn2'
         46        SEND_VAL                                                 '4+2+5+%2A+%2B+1+3+2+%2A+%2B+%2F'
         47        DO_FCALL                                      0  $9      
         48        ECHO                                                     $9
         49        ECHO                                                     '%0A'
   36    50        INIT_FCALL                                               'evalrpn3'
         51        SEND_VAL                                                 '-4+5+%2B'
         52        DO_FCALL                                      0  $10     
         53        ECHO                                                     $10
         54        ECHO                                                     '%0A'
   37    55        INIT_FCALL                                               'evalrpn3'
         56        SEND_VAL                                                 '5+++2+%2F'
         57        DO_FCALL                                      0  $11     
         58        ECHO                                                     $11
         59        ECHO                                                     '%0A'
   38    60        INIT_FCALL                                               'evalrpn3'
         61        SEND_VAL                                                 '5+2.5++++%2F'
         62        DO_FCALL                                      0  $12     
         63        ECHO                                                     $12
         64        ECHO                                                     '%0A'
   39    65        INIT_FCALL                                               'evalrpn3'
         66        SEND_VAL                                                 '5+1+2+%2B+4+%2A+3+-+%2B'
         67        DO_FCALL                                      0  $13     
         68        ECHO                                                     $13
         69        ECHO                                                     '%0A'
   40    70        INIT_FCALL                                               'evalrpn3'
         71        SEND_VAL                                                 '4+2+5+%2A+%2B+1+3+2+%2A+%2B+%2F'
         72        DO_FCALL                                      0  $14     
         73        ECHO                                                     $14
         74        ECHO                                                     '%0A'
         75      > RETURN                                                   1

Function evalrpn:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 8
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 17
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 8
Branch analysis from position: 49
Branch analysis from position: 8
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 8
Branch analysis from position: 49
Branch analysis from position: 8
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 33
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 8
Branch analysis from position: 49
Branch analysis from position: 8
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 8
Branch analysis from position: 49
Branch analysis from position: 8
filename:       /in/N81qc
function name:  evalRPN
number of ops:  51
compiled vars:  !0 = $input, !1 = $v, !2 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    3     1        FETCH_W                      global              $3      '_GET'
          2        ASSIGN_DIM                                               $3, 'a'
          3        OP_DATA                                                  !0
    4     4        FETCH_R                      global              ~5      '_GET'
          5        FETCH_DIM_R                                      ~6      ~5, 'a'
          6        ASSIGN                                                   !1, ~6
    5     7      > JMP                                                      ->43
    6     8    >   FETCH_DIM_R                                      ~8      !2, 1
          9        FETCH_DIM_R                                      ~9      !2, 4
         10        IS_EQUAL                                                 ~9, '%2B'
         11      > JMPZ                                                     ~10, ->17
         12    >   FETCH_DIM_R                                      ~11     !2, 2
         13        FETCH_DIM_R                                      ~12     !2, 3
         14        ADD                                              ~13     ~11, ~12
         15        QM_ASSIGN                                        ~14     ~13
         16      > JMP                                                      ->39
         17    >   FETCH_DIM_R                                      ~15     !2, 4
         18        IS_EQUAL                                                 ~15, '-'
         19      > JMPZ                                                     ~16, ->25
         20    >   FETCH_DIM_R                                      ~17     !2, 2
         21        FETCH_DIM_R                                      ~18     !2, 3
         22        SUB                                              ~19     ~17, ~18
         23        QM_ASSIGN                                        ~20     ~19
         24      > JMP                                                      ->38
         25    >   FETCH_DIM_R                                      ~21     !2, 4
         26        IS_EQUAL                                                 ~21, '%2A'
         27      > JMPZ                                                     ~22, ->33
         28    >   FETCH_DIM_R                                      ~23     !2, 2
         29        FETCH_DIM_R                                      ~24     !2, 3
         30        MUL                                              ~25     ~23, ~24
         31        QM_ASSIGN                                        ~26     ~25
         32      > JMP                                                      ->37
         33    >   FETCH_DIM_R                                      ~27     !2, 2
         34        FETCH_DIM_R                                      ~28     !2, 3
         35        DIV                                              ~29     ~27, ~28
         36        QM_ASSIGN                                        ~26     ~29
         37    >   QM_ASSIGN                                        ~20     ~26
         38    >   QM_ASSIGN                                        ~14     ~20
         39    >   CONCAT                                           ~30     ~8, ~14
         40        FETCH_DIM_R                                      ~31     !2, 5
         41        CONCAT                                           ~32     ~30, ~31
         42        ASSIGN                                                   !1, ~32
    5    43    >   INIT_FCALL                                               'preg_match'
         44        SEND_VAL                                                 '%7E%28.%2A%3F%29%28-%3F%5B%5Cd.%5D%2B%29+%2B%28-%3F%5B%5Cd.%5D%2B%29+%2B%28%5B%2A%2F%2B-%5D%29%28.%2A%29%7E'
         45        SEND_VAR                                                 !1
         46        SEND_REF                                                 !2
         47        DO_ICALL                                         $34     
         48      > JMPNZ                                                    $34, ->8
    7    49    > > RETURN                                                   !1
    8    50*     > RETURN                                                   null

End of function evalrpn

Function evalrpn2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 7
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/N81qc
function name:  evalRPN2
number of ops:  19
compiled vars:  !0 = $input, !1 = $v, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1        FETCH_W                      global              $3      '_GET'
          2        ASSIGN_DIM                                               $3, 'a'
          3        OP_DATA                                                  !0
   11     4        FETCH_R                      global              ~5      '_GET'
          5        FETCH_DIM_R                                      ~6      ~5, 'a'
          6        ASSIGN                                                   !1, ~6
   12     7    >   INIT_FCALL                                               'preg_replace_callback'
   13     8        SEND_VAL                                                 '%7E%28-%3F%5B%5Cd.%5D%2B%29+%2B%28-%3F%5B%5Cd.%5D%2B%29+%2B%28%5B%2A%2F%2B-%5D%29%7E'
   14     9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FN81qc%3A14%240'
         10        SEND_VAL                                                 ~8
   15    11        SEND_VAR                                                 !1
         12        SEND_VAL                                                 9
         13        SEND_REF                                                 !2
         14        DO_ICALL                                         $9      
   12    15        ASSIGN                                                   !1, $9
   16    16      > JMPNZ                                                    !2, ->7
   17    17    > > RETURN                                                   !1
   18    18*     > RETURN                                                   null

End of function evalrpn2

Function %00%7Bclosure%7D%2Fin%2FN81qc%3A14%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 17
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N81qc
function name:  {closure}
number of ops:  33
compiled vars:  !0 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        FETCH_DIM_R                                      ~1      !0, 3
          2        IS_EQUAL                                                 ~1, '%2B'
          3      > JMPZ                                                     ~2, ->9
          4    >   FETCH_DIM_R                                      ~3      !0, 1
          5        FETCH_DIM_R                                      ~4      !0, 2
          6        ADD                                              ~5      ~3, ~4
          7        QM_ASSIGN                                        ~6      ~5
          8      > JMP                                                      ->31
          9    >   FETCH_DIM_R                                      ~7      !0, 3
         10        IS_EQUAL                                                 ~7, '-'
         11      > JMPZ                                                     ~8, ->17
         12    >   FETCH_DIM_R                                      ~9      !0, 1
         13        FETCH_DIM_R                                      ~10     !0, 2
         14        SUB                                              ~11     ~9, ~10
         15        QM_ASSIGN                                        ~12     ~11
         16      > JMP                                                      ->30
         17    >   FETCH_DIM_R                                      ~13     !0, 3
         18        IS_EQUAL                                                 ~13, '%2A'
         19      > JMPZ                                                     ~14, ->25
         20    >   FETCH_DIM_R                                      ~15     !0, 1
         21        FETCH_DIM_R                                      ~16     !0, 2
         22        MUL                                              ~17     ~15, ~16
         23        QM_ASSIGN                                        ~18     ~17
         24      > JMP                                                      ->29
         25    >   FETCH_DIM_R                                      ~19     !0, 1
         26        FETCH_DIM_R                                      ~20     !0, 2
         27        DIV                                              ~21     ~19, ~20
         28        QM_ASSIGN                                        ~18     ~21
         29    >   QM_ASSIGN                                        ~12     ~18
         30    >   QM_ASSIGN                                        ~6      ~12
         31    > > RETURN                                                   ~6
         32*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FN81qc%3A14%240

Function evalrpn3:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 48
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 48
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 31
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 36
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 41
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
filename:       /in/N81qc
function name:  evalRPN3
number of ops:  52
compiled vars:  !0 = $input, !1 = $v, !2 = $s, !3 = $e, !4 = $d
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   20     1        FETCH_W                      global              $5      '_GET'
          2        ASSIGN_DIM                                               $5, 'a'
          3        OP_DATA                                                  !0
   21     4        INIT_FCALL                                               'preg_split'
          5        SEND_VAL                                                 '%7E+%2B%7E'
          6        FETCH_R                      global              ~7      '_GET'
          7        FETCH_DIM_R                                      ~8      ~7, 'a'
          8        SEND_VAL                                                 ~8
          9        DO_ICALL                                         $9      
         10      > FE_RESET_R                                       $10     $9, ->48
         11    > > FE_FETCH_R                                               $10, !1, ->48
   22    12    >   CAST                                          5  ~11     !1
         13      > JMPZ                                                     ~11, ->17
         14    >   ASSIGN_DIM                                               !2
         15        OP_DATA                                                  !1
         16      > JMP                                                      ->47
         17    >   INIT_FCALL                                               'array_splice'
         18        SEND_REF                                                 !2
         19        SEND_VAL                                                 -2
         20        DO_ICALL                                         $13     
         21        FETCH_LIST_R                                     $14     $13, 0
         22        ASSIGN                                                   !3, $14
         23        FETCH_LIST_R                                     $16     $13, 1
         24        ASSIGN                                                   !4, $16
         25        FREE                                                     $13
         26        IS_EQUAL                                                 !1, '%2B'
         27      > JMPZ                                                     ~19, ->31
         28    >   ADD                                              ~20     !3, !4
         29        QM_ASSIGN                                        ~21     ~20
         30      > JMP                                                      ->45
         31    >   IS_EQUAL                                                 !1, '-'
         32      > JMPZ                                                     ~22, ->36
         33    >   SUB                                              ~23     !3, !4
         34        QM_ASSIGN                                        ~24     ~23
         35      > JMP                                                      ->44
         36    >   IS_EQUAL                                                 !1, '%2A'
         37      > JMPZ                                                     ~25, ->41
         38    >   MUL                                              ~26     !3, !4
         39        QM_ASSIGN                                        ~27     ~26
         40      > JMP                                                      ->43
         41    >   DIV                                              ~28     !3, !4
         42        QM_ASSIGN                                        ~27     ~28
         43    >   QM_ASSIGN                                        ~24     ~27
         44    >   QM_ASSIGN                                        ~21     ~24
         45    >   ASSIGN_DIM                                               !2
         46        OP_DATA                                                  ~21
   21    47    > > JMP                                                      ->11
         48    >   FE_FREE                                                  $10
   23    49        FETCH_DIM_R                                      ~29     !2, 0
         50      > RETURN                                                   ~29
   24    51*     > RETURN                                                   null

End of function evalrpn3

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
196.26 ms | 1423 KiB | 36 Q