3v4l.org

run code in 300+ PHP versions simultaneously
<?php $expression = "5+4^3^2^1"; if (!preg_match('~^-?\d*\.?\d+([*/+-^]-?\d*\.?\d+)*$~', $expression)) { echo "invalid expression"; } else { $components = preg_split('~(?<=\d)([*/+-^])~', $expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); var_export($components); // ['5','+','4','^','3','^','2','^','1'] while (($index = array_search('^', $components)) !== false) { array_splice($components, $index - 1, 3, pow($components[$index - 1], $components[$index + 1])); var_export($components); // ['5', '+', 64, '^', '2', '^', '1'] // ['5', '+' ,4096, '^', '1'] // ['5', '+', 4096] } while (($index = array_search('*', $components)) !== false) { array_splice($components, $index - 1, 3, $components[$index - 1] * $components[$index + 1]); var_export($components); } while (($index = array_search('/', $components)) !== false) { array_splice($components, $index - 1, 3, $components[$index - 1] / $components[$index + 1]); } while (($index = array_search('+', $components)) !== false) { array_splice($components, $index - 1, 3, $components[$index - 1] + $components[$index + 1]); // [4101] } while (($index = array_search('-', $components)) !== false) { array_splice($components, $index - 1, 3, $components[$index - 1] - $components[$index + 1]); } echo $components[0]; // 4101 }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 45, Position 2 = 20
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
2 jumps found. (Code = 44) Position 1 = 68, Position 2 = 46
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
2 jumps found. (Code = 44) Position 1 = 88, Position 2 = 69
Branch analysis from position: 88
1 jumps found. (Code = 42) Position 1 = 101
Branch analysis from position: 101
2 jumps found. (Code = 44) Position 1 = 108, Position 2 = 89
Branch analysis from position: 108
1 jumps found. (Code = 42) Position 1 = 121
Branch analysis from position: 121
2 jumps found. (Code = 44) Position 1 = 128, Position 2 = 109
Branch analysis from position: 128
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 109
2 jumps found. (Code = 44) Position 1 = 128, Position 2 = 109
Branch analysis from position: 128
Branch analysis from position: 109
Branch analysis from position: 89
2 jumps found. (Code = 44) Position 1 = 108, Position 2 = 89
Branch analysis from position: 108
Branch analysis from position: 89
Branch analysis from position: 69
2 jumps found. (Code = 44) Position 1 = 88, Position 2 = 69
Branch analysis from position: 88
Branch analysis from position: 69
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 68, Position 2 = 46
Branch analysis from position: 68
Branch analysis from position: 46
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 45, Position 2 = 20
Branch analysis from position: 45
Branch analysis from position: 20
filename:       /in/c5PIX
function name:  (null)
number of ops:  131
compiled vars:  !0 = $expression, !1 = $components, !2 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '5%2B4%5E3%5E2%5E1'
    4     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%7E%5E-%3F%5Cd%2A%5C.%3F%5Cd%2B%28%5B%2A%2F%2B-%5E%5D-%3F%5Cd%2A%5C.%3F%5Cd%2B%29%2A%24%7E'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $4      
          5        BOOL_NOT                                         ~5      $4
          6      > JMPZ                                                     ~5, ->9
    5     7    >   ECHO                                                     'invalid+expression'
    4     8      > JMP                                                      ->130
    7     9    >   INIT_FCALL                                               'preg_split'
         10        SEND_VAL                                                 '%7E%28%3F%3C%3D%5Cd%29%28%5B%2A%2F%2B-%5E%5D%29%7E'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 0
         13        SEND_VAL                                                 3
         14        DO_ICALL                                         $6      
         15        ASSIGN                                                   !1, $6
    8    16        INIT_FCALL                                               'var_export'
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                                 
    9    19      > JMP                                                      ->38
   10    20    >   INIT_FCALL                                               'array_splice'
         21        SEND_REF                                                 !1
         22        SUB                                              ~9      !2, 1
         23        SEND_VAL                                                 ~9
         24        SEND_VAL                                                 3
         25        INIT_FCALL                                               'pow'
         26        SUB                                              ~10     !2, 1
         27        FETCH_DIM_R                                      ~11     !1, ~10
         28        SEND_VAL                                                 ~11
         29        ADD                                              ~12     !2, 1
         30        FETCH_DIM_R                                      ~13     !1, ~12
         31        SEND_VAL                                                 ~13
         32        DO_ICALL                                         $14     
         33        SEND_VAR                                                 $14
         34        DO_ICALL                                                 
   11    35        INIT_FCALL                                               'var_export'
         36        SEND_VAR                                                 !1
         37        DO_ICALL                                                 
    9    38    >   INIT_FCALL                                               'array_search'
         39        SEND_VAL                                                 '%5E'
         40        SEND_VAR                                                 !1
         41        DO_ICALL                                         $17     
         42        ASSIGN                                           ~18     !2, $17
         43        TYPE_CHECK                                  1018          ~18
         44      > JMPNZ                                                    ~19, ->20
   16    45    > > JMP                                                      ->61
   17    46    >   INIT_FCALL                                               'array_splice'
         47        SEND_REF                                                 !1
         48        SUB                                              ~20     !2, 1
         49        SEND_VAL                                                 ~20
         50        SEND_VAL                                                 3
         51        SUB                                              ~21     !2, 1
         52        FETCH_DIM_R                                      ~22     !1, ~21
         53        ADD                                              ~23     !2, 1
         54        FETCH_DIM_R                                      ~24     !1, ~23
         55        MUL                                              ~25     ~22, ~24
         56        SEND_VAL                                                 ~25
         57        DO_ICALL                                                 
   18    58        INIT_FCALL                                               'var_export'
         59        SEND_VAR                                                 !1
         60        DO_ICALL                                                 
   16    61    >   INIT_FCALL                                               'array_search'
         62        SEND_VAL                                                 '%2A'
         63        SEND_VAR                                                 !1
         64        DO_ICALL                                         $28     
         65        ASSIGN                                           ~29     !2, $28
         66        TYPE_CHECK                                  1018          ~29
         67      > JMPNZ                                                    ~30, ->46
   20    68    > > JMP                                                      ->81
   21    69    >   INIT_FCALL                                               'array_splice'
         70        SEND_REF                                                 !1
         71        SUB                                              ~31     !2, 1
         72        SEND_VAL                                                 ~31
         73        SEND_VAL                                                 3
         74        SUB                                              ~32     !2, 1
         75        FETCH_DIM_R                                      ~33     !1, ~32
         76        ADD                                              ~34     !2, 1
         77        FETCH_DIM_R                                      ~35     !1, ~34
         78        DIV                                              ~36     ~33, ~35
         79        SEND_VAL                                                 ~36
         80        DO_ICALL                                                 
   20    81    >   INIT_FCALL                                               'array_search'
         82        SEND_VAL                                                 '%2F'
         83        SEND_VAR                                                 !1
         84        DO_ICALL                                         $38     
         85        ASSIGN                                           ~39     !2, $38
         86        TYPE_CHECK                                  1018          ~39
         87      > JMPNZ                                                    ~40, ->69
   23    88    > > JMP                                                      ->101
   24    89    >   INIT_FCALL                                               'array_splice'
         90        SEND_REF                                                 !1
         91        SUB                                              ~41     !2, 1
         92        SEND_VAL                                                 ~41
         93        SEND_VAL                                                 3
         94        SUB                                              ~42     !2, 1
         95        FETCH_DIM_R                                      ~43     !1, ~42
         96        ADD                                              ~44     !2, 1
         97        FETCH_DIM_R                                      ~45     !1, ~44
         98        ADD                                              ~46     ~43, ~45
         99        SEND_VAL                                                 ~46
        100        DO_ICALL                                                 
   23   101    >   INIT_FCALL                                               'array_search'
        102        SEND_VAL                                                 '%2B'
        103        SEND_VAR                                                 !1
        104        DO_ICALL                                         $48     
        105        ASSIGN                                           ~49     !2, $48
        106        TYPE_CHECK                                  1018          ~49
        107      > JMPNZ                                                    ~50, ->89
   27   108    > > JMP                                                      ->121
   28   109    >   INIT_FCALL                                               'array_splice'
        110        SEND_REF                                                 !1
        111        SUB                                              ~51     !2, 1
        112        SEND_VAL                                                 ~51
        113        SEND_VAL                                                 3
        114        SUB                                              ~52     !2, 1
        115        FETCH_DIM_R                                      ~53     !1, ~52
        116        ADD                                              ~54     !2, 1
        117        FETCH_DIM_R                                      ~55     !1, ~54
        118        SUB                                              ~56     ~53, ~55
        119        SEND_VAL                                                 ~56
        120        DO_ICALL                                                 
   27   121    >   INIT_FCALL                                               'array_search'
        122        SEND_VAL                                                 '-'
        123        SEND_VAR                                                 !1
        124        DO_ICALL                                         $58     
        125        ASSIGN                                           ~59     !2, $58
        126        TYPE_CHECK                                  1018          ~59
        127      > JMPNZ                                                    ~60, ->109
   30   128    >   FETCH_DIM_R                                      ~61     !1, 0
        129        ECHO                                                     ~61
   31   130    > > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.32 ms | 1016 KiB | 19 Q