3v4l.org

run code in 300+ PHP versions simultaneously
<?php //Hi, this is actually possible already in PHP already, and doesn't need any extra functionality. // You can specify a function to be called to combine multiple arguments into a single // value inside case statements like this: function f($value, $access = null) { $knownAccessTypes = [ 'read-only', 'read-write' ]; if (in_array($access, $knownAccessTypes)) { return $value.$access; } return $value; } function multiSwitchFunction($value, $access = null) { switch (f($value, $access)) { case f('a', 'read-only'): return 'a r'; case f('a', 'read-write'): return 'a w'; case f('a'): return 'a default'; case f('b', 'read-only'): return 'b r'; case f('b', 'read-write'): return 'b w '; case f('c', 'read-only'): return 'c r'; case f('c', 'read-write'): return 'c w'; case f('d', 'read-only'): return 'd r'; case f('d', 'read-write'): return 'd w' ; default: return "unknown"; } } var_dump(multiSwitchFunction('a', 'read-write')); var_dump(multiSwitchFunction('a', 'foobar')); var_dump(multiSwitchFunction('d', 'read-write')); var_dump(multiSwitchFunction('d', 'foobar'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sk5l3
function name:  (null)
number of ops:  29
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'multiswitchfunction'
          2        SEND_VAL                                                 'a'
          3        SEND_VAL                                                 'read-write'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   44     7        INIT_FCALL                                               'var_dump'
          8        INIT_FCALL                                               'multiswitchfunction'
          9        SEND_VAL                                                 'a'
         10        SEND_VAL                                                 'foobar'
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
   45    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL                                               'multiswitchfunction'
         16        SEND_VAL                                                 'd'
         17        SEND_VAL                                                 'read-write'
         18        DO_FCALL                                      0  $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
   46    21        INIT_FCALL                                               'var_dump'
         22        INIT_FCALL                                               'multiswitchfunction'
         23        SEND_VAL                                                 'd'
         24        SEND_VAL                                                 'foobar'
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        DO_ICALL                                                 
         28      > RETURN                                                   1

Function f:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sk5l3
function name:  f
number of ops:  12
compiled vars:  !0 = $value, !1 = $access, !2 = $knownAccessTypes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   11     2        ASSIGN                                                   !2, <array>
   16     3        INIT_FCALL                                               'in_array'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !2
          6        DO_ICALL                                         $4      
          7      > JMPZ                                                     $4, ->10
   17     8    >   CONCAT                                           ~5      !0, !1
          9      > RETURN                                                   ~5
   20    10    > > RETURN                                                   !0
   21    11*     > RETURN                                                   null

End of function f

Function multiswitchfunction:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 60
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 62
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 64
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 66
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 68
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 70
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 72
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 74
Branch analysis from position: 53
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 76
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 72
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 70
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sk5l3
function name:  multiSwitchFunction
number of ops:  82
compiled vars:  !0 = $value, !1 = $access
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   25     2        INIT_FCALL                                               'f'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $2      
   26     6        INIT_FCALL                                               'f'
          7        SEND_VAL                                                 'a'
          8        SEND_VAL                                                 'read-only'
          9        DO_FCALL                                      0  $4      
         10        CASE                                                     $2, $4
         11      > JMPNZ                                                    ~3, ->60
   27    12    >   INIT_FCALL                                               'f'
         13        SEND_VAL                                                 'a'
         14        SEND_VAL                                                 'read-write'
         15        DO_FCALL                                      0  $5      
         16        CASE                                                     $2, $5
         17      > JMPNZ                                                    ~3, ->62
   28    18    >   INIT_FCALL                                               'f'
         19        SEND_VAL                                                 'a'
         20        DO_FCALL                                      0  $6      
         21        CASE                                                     $2, $6
         22      > JMPNZ                                                    ~3, ->64
   30    23    >   INIT_FCALL                                               'f'
         24        SEND_VAL                                                 'b'
         25        SEND_VAL                                                 'read-only'
         26        DO_FCALL                                      0  $7      
         27        CASE                                                     $2, $7
         28      > JMPNZ                                                    ~3, ->66
   31    29    >   INIT_FCALL                                               'f'
         30        SEND_VAL                                                 'b'
         31        SEND_VAL                                                 'read-write'
         32        DO_FCALL                                      0  $8      
         33        CASE                                                     $2, $8
         34      > JMPNZ                                                    ~3, ->68
   33    35    >   INIT_FCALL                                               'f'
         36        SEND_VAL                                                 'c'
         37        SEND_VAL                                                 'read-only'
         38        DO_FCALL                                      0  $9      
         39        CASE                                                     $2, $9
         40      > JMPNZ                                                    ~3, ->70
   34    41    >   INIT_FCALL                                               'f'
         42        SEND_VAL                                                 'c'
         43        SEND_VAL                                                 'read-write'
         44        DO_FCALL                                      0  $10     
         45        CASE                                                     $2, $10
         46      > JMPNZ                                                    ~3, ->72
   36    47    >   INIT_FCALL                                               'f'
         48        SEND_VAL                                                 'd'
         49        SEND_VAL                                                 'read-only'
         50        DO_FCALL                                      0  $11     
         51        CASE                                                     $2, $11
         52      > JMPNZ                                                    ~3, ->74
   37    53    >   INIT_FCALL                                               'f'
         54        SEND_VAL                                                 'd'
         55        SEND_VAL                                                 'read-write'
         56        DO_FCALL                                      0  $12     
         57        CASE                                                     $2, $12
         58      > JMPNZ                                                    ~3, ->76
         59    > > JMP                                                      ->78
   26    60    >   FREE                                                     $2
         61      > RETURN                                                   'a+r'
   27    62    >   FREE                                                     $2
         63      > RETURN                                                   'a+w'
   28    64    >   FREE                                                     $2
         65      > RETURN                                                   'a+default'
   30    66    >   FREE                                                     $2
         67      > RETURN                                                   'b+r'
   31    68    >   FREE                                                     $2
         69      > RETURN                                                   'b+w+'
   33    70    >   FREE                                                     $2
         71      > RETURN                                                   'c+r'
   34    72    >   FREE                                                     $2
         73      > RETURN                                                   'c+w'
   36    74    >   FREE                                                     $2
         75      > RETURN                                                   'd+r'
   37    76    >   FREE                                                     $2
         77      > RETURN                                                   'd+w'
   39    78    >   FREE                                                     $2
         79      > RETURN                                                   'unknown'
         80*       FREE                                                     $2
   41    81*     > RETURN                                                   null

End of function multiswitchfunction

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.08 ms | 1410 KiB | 31 Q