3v4l.org

run code in 300+ PHP versions simultaneously
<?php function add($stack, $nb1, $nb2) { array_push($stack, $nb1 + $nb2); $result = $nb1 + $nb2; echo $nb1 . ' + ' . $nb2 . ' = ' . $result . "\n"; } function sub($stack, $nb1, $nb2) { array_push($stack, $nb1 - $nb2); $result = $nb1 - $nb2; echo $nb1 . ' - ' . $nb2 . ' = ' . $result . "\n"; } function div($stack, $nb1, $nb2) { array_push($stack, $nb1 / $nb2); $result = $nb1 / $nb2; echo $nb1 . ' / ' . $nb2 . ' = ' . $result . "\n"; } function mul($stack, $nb1, $nb2) { array_push($stack, $nb1 * $nb2); $result = $nb1 * $nb2; echo $nb1 . ' * ' . $nb2 . ' = ' . $result . "\n"; } function calc($input) { $stack = array(); $token = explode(" ", trim($input)); $count = count($token); // echo $count . "\n"; // print_r($token); for ($i = 0; $i < $count; $i++) { $tokenNUm = ""; if (is_numeric($token[$i])) { array_push($stack, $token[$i]); print_r($token); } else { $nb2 = end($stack); array_pop($stack); $nb1 = end($stack); array_pop($stack); echo "nb1 : ". $nb1 . "\n"; echo "nb2 : ". $nb2 . "\n"; switch($token[$i]) { case '+': add($stack, $nb1, $nb2); break; case '-': sub($stack, $nb1, $nb2); break; case '/': div($stack, $nb1, $nb2); break; case '*': mul($stack, $nb1, $nb2); break; default: die('Error'); } } } return end($stack); } $argv[1] = "1 + 2"; echo "Final result = " . calc($argv[1]) . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HGkSc
function name:  (null)
number of ops:  10
compiled vars:  !0 = $argv
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   ASSIGN_DIM                                               !0, 1
          1        OP_DATA                                                  '1+%2B+2'
   77     2        INIT_FCALL                                               'calc'
          3        FETCH_DIM_R                                      ~2      !0, 1
          4        SEND_VAL                                                 ~2
          5        DO_FCALL                                      0  $3      
          6        CONCAT                                           ~4      'Final+result+%3D+', $3
          7        CONCAT                                           ~5      ~4, '%0A'
          8        ECHO                                                     ~5
          9      > RETURN                                                   1

Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HGkSc
function name:  add
number of ops:  17
compiled vars:  !0 = $stack, !1 = $nb1, !2 = $nb2, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    5     3        INIT_FCALL                                               'array_push'
          4        SEND_REF                                                 !0
          5        ADD                                              ~4      !1, !2
          6        SEND_VAL                                                 ~4
          7        DO_ICALL                                                 
    6     8        ADD                                              ~6      !1, !2
          9        ASSIGN                                                   !3, ~6
    7    10        CONCAT                                           ~8      !1, '+%2B+'
         11        CONCAT                                           ~9      ~8, !2
         12        CONCAT                                           ~10     ~9, '+%3D+'
         13        CONCAT                                           ~11     ~10, !3
         14        CONCAT                                           ~12     ~11, '%0A'
         15        ECHO                                                     ~12
    8    16      > RETURN                                                   null

End of function add

Function sub:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HGkSc
function name:  sub
number of ops:  17
compiled vars:  !0 = $stack, !1 = $nb1, !2 = $nb2, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   12     3        INIT_FCALL                                               'array_push'
          4        SEND_REF                                                 !0
          5        SUB                                              ~4      !1, !2
          6        SEND_VAL                                                 ~4
          7        DO_ICALL                                                 
   13     8        SUB                                              ~6      !1, !2
          9        ASSIGN                                                   !3, ~6
   14    10        CONCAT                                           ~8      !1, '+-+'
         11        CONCAT                                           ~9      ~8, !2
         12        CONCAT                                           ~10     ~9, '+%3D+'
         13        CONCAT                                           ~11     ~10, !3
         14        CONCAT                                           ~12     ~11, '%0A'
         15        ECHO                                                     ~12
   15    16      > RETURN                                                   null

End of function sub

Function div:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HGkSc
function name:  div
number of ops:  17
compiled vars:  !0 = $stack, !1 = $nb1, !2 = $nb2, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   19     3        INIT_FCALL                                               'array_push'
          4        SEND_REF                                                 !0
          5        DIV                                              ~4      !1, !2
          6        SEND_VAL                                                 ~4
          7        DO_ICALL                                                 
   20     8        DIV                                              ~6      !1, !2
          9        ASSIGN                                                   !3, ~6
   21    10        CONCAT                                           ~8      !1, '+%2F+'
         11        CONCAT                                           ~9      ~8, !2
         12        CONCAT                                           ~10     ~9, '+%3D+'
         13        CONCAT                                           ~11     ~10, !3
         14        CONCAT                                           ~12     ~11, '%0A'
         15        ECHO                                                     ~12
   22    16      > RETURN                                                   null

End of function div

Function mul:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HGkSc
function name:  mul
number of ops:  17
compiled vars:  !0 = $stack, !1 = $nb1, !2 = $nb2, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   26     3        INIT_FCALL                                               'array_push'
          4        SEND_REF                                                 !0
          5        MUL                                              ~4      !1, !2
          6        SEND_VAL                                                 ~4
          7        DO_ICALL                                                 
   27     8        MUL                                              ~6      !1, !2
          9        ASSIGN                                                   !3, ~6
   28    10        CONCAT                                           ~8      !1, '+%2A+'
         11        CONCAT                                           ~9      ~8, !2
         12        CONCAT                                           ~10     ~9, '+%3D+'
         13        CONCAT                                           ~11     ~10, !3
         14        CONCAT                                           ~12     ~11, '%0A'
         15        ECHO                                                     ~12
   29    16      > RETURN                                                   null

End of function mul

Function calc:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
2 jumps found. (Code = 44) Position 1 = 89, Position 2 = 14
Branch analysis from position: 89
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 29
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 86
Branch analysis from position: 86
2 jumps found. (Code = 44) Position 1 = 89, Position 2 = 14
Branch analysis from position: 89
Branch analysis from position: 14
Branch analysis from position: 29
6 jumps found. (Code = 188) Position 1 = 60, Position 2 = 66, Position 3 = 72, Position 4 = 78, Position 5 = 84, Position 6 = 51
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 85
Branch analysis from position: 85
2 jumps found. (Code = 44) Position 1 = 89, Position 2 = 14
Branch analysis from position: 89
Branch analysis from position: 14
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 85
Branch analysis from position: 85
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 85
Branch analysis from position: 85
Branch analysis from position: 78
1 jumps found. (Code = 42) Position 1 = 85
Branch analysis from position: 85
Branch analysis from position: 84
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 60
Branch analysis from position: 53
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 66
Branch analysis from position: 55
2 jumps found. (Code = 44) Position 1 = 57, Position 2 = 72
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 78
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
Branch analysis from position: 78
Branch analysis from position: 72
Branch analysis from position: 66
Branch analysis from position: 60
filename:       /in/HGkSc
function name:  calc
number of ops:  94
compiled vars:  !0 = $input, !1 = $stack, !2 = $token, !3 = $count, !4 = $i, !5 = $tokenNUm, !6 = $nb2, !7 = $nb1
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   33     1        ASSIGN                                                   !1, <array>
   34     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '+'
          4        INIT_FCALL                                               'trim'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $9      
          7        SEND_VAR                                                 $9
          8        DO_ICALL                                         $10     
          9        ASSIGN                                                   !2, $10
   35    10        COUNT                                            ~12     !2
         11        ASSIGN                                                   !3, ~12
   39    12        ASSIGN                                                   !4, 0
         13      > JMP                                                      ->87
   40    14    >   ASSIGN                                                   !5, ''
   42    15        INIT_FCALL                                               'is_numeric'
         16        FETCH_DIM_R                                      ~16     !2, !4
         17        SEND_VAL                                                 ~16
         18        DO_ICALL                                         $17     
         19      > JMPZ                                                     $17, ->29
   43    20    >   INIT_FCALL                                               'array_push'
         21        SEND_REF                                                 !1
         22        FETCH_DIM_R                                      ~18     !2, !4
         23        SEND_VAL                                                 ~18
         24        DO_ICALL                                                 
   44    25        INIT_FCALL                                               'print_r'
         26        SEND_VAR                                                 !2
         27        DO_ICALL                                                 
         28      > JMP                                                      ->86
   46    29    >   INIT_FCALL                                               'end'
         30        SEND_REF                                                 !1
         31        DO_ICALL                                         $21     
         32        ASSIGN                                                   !6, $21
   47    33        INIT_FCALL                                               'array_pop'
         34        SEND_REF                                                 !1
         35        DO_ICALL                                                 
   48    36        INIT_FCALL                                               'end'
         37        SEND_REF                                                 !1
         38        DO_ICALL                                         $24     
         39        ASSIGN                                                   !7, $24
   49    40        INIT_FCALL                                               'array_pop'
         41        SEND_REF                                                 !1
         42        DO_ICALL                                                 
   50    43        CONCAT                                           ~27     'nb1+%3A+', !7
         44        CONCAT                                           ~28     ~27, '%0A'
         45        ECHO                                                     ~28
   51    46        CONCAT                                           ~29     'nb2+%3A+', !6
         47        CONCAT                                           ~30     ~29, '%0A'
         48        ECHO                                                     ~30
   53    49        FETCH_DIM_R                                      ~31     !2, !4
         50      > SWITCH_STRING                                            ~31, [ '%2B':->60, '-':->66, '%2F':->72, '%2A':->78, ], ->84
   54    51    >   CASE                                                     ~31, '%2B'
         52      > JMPNZ                                                    ~32, ->60
   57    53    >   CASE                                                     ~31, '-'
         54      > JMPNZ                                                    ~32, ->66
   60    55    >   CASE                                                     ~31, '%2F'
         56      > JMPNZ                                                    ~32, ->72
   63    57    >   CASE                                                     ~31, '%2A'
         58      > JMPNZ                                                    ~32, ->78
         59    > > JMP                                                      ->84
   55    60    >   INIT_FCALL                                               'add'
         61        SEND_VAR                                                 !1
         62        SEND_VAR                                                 !7
         63        SEND_VAR                                                 !6
         64        DO_FCALL                                      0          
   56    65      > JMP                                                      ->85
   58    66    >   INIT_FCALL                                               'sub'
         67        SEND_VAR                                                 !1
         68        SEND_VAR                                                 !7
         69        SEND_VAR                                                 !6
         70        DO_FCALL                                      0          
   59    71      > JMP                                                      ->85
   61    72    >   INIT_FCALL                                               'div'
         73        SEND_VAR                                                 !1
         74        SEND_VAR                                                 !7
         75        SEND_VAR                                                 !6
         76        DO_FCALL                                      0          
   62    77      > JMP                                                      ->85
   64    78    >   INIT_FCALL                                               'mul'
         79        SEND_VAR                                                 !1
         80        SEND_VAR                                                 !7
         81        SEND_VAR                                                 !6
         82        DO_FCALL                                      0          
   65    83      > JMP                                                      ->85
   67    84    > > EXIT                                                     'Error'
         85    >   FREE                                                     ~31
   39    86    >   PRE_INC                                                  !4
         87    >   IS_SMALLER                                               !4, !3
         88      > JMPNZ                                                    ~38, ->14
   72    89    >   INIT_FCALL                                               'end'
         90        SEND_REF                                                 !1
         91        DO_ICALL                                         $39     
         92      > RETURN                                                   $39
   73    93*     > RETURN                                                   null

End of function calc

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.52 ms | 1425 KiB | 32 Q