3v4l.org

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

End of function calc

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
180.99 ms | 3407 KiB | 24 Q