3v4l.org

run code in 300+ PHP versions simultaneously
<?php // produces the expected result echo transpose("F#sus7/C#",3); function transpose($chord,$transpose) { // the chords $chords = array("C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"); $result = ""; // get root tone $root_arr = explode("/",$chord); $root = strtoupper($root_arr[0]); // the chord is the first character and a # if there is one $root = $root[0].((strpos($root, "#") !== false)?"#":""); // get any extra info $root_extra_info = str_replace("#","",substr($root_arr[0],1)); // assuming that extra info does not have any # // find the index on chords array $root_index = array_search($root,$chords); // transpose the values and modulo by 12 so we always point to existing indexes in our array $root_transpose_index = floor(($root_index + $transpose) % 12); if ($root_transpose_index < 0) { $root_transpose_index += 12; } $result.= $chords[$root_transpose_index].$root_extra_info; if(count($root_arr)>1) { // get the non root tone $non_root = $root_arr[1]; // the chord is the first character and a # if there is one $non_root = strtoupper($non_root[0]).((strpos($non_root, "#") !== false)?"#":""); // get any extra info $non_root_extra_info = str_replace("#","",substr($root_arr[1],1)); // assuming that extra info does not have any # // find the index on chords array $non_root_index = array_search($non_root,$chords); // transpose the values and modulo by 12 so we always point to existing indexes in our array $non_root_transpose_index = floor(($non_root_index + $transpose) % 12); if ($non_root_transpose_index < 0) { $non_root_transpose_index += 12; } $result.= "/".$chords[$non_root_transpose_index].$non_root_extra_info; } return $result; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Cd9Pg
function name:  (null)
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   INIT_FCALL_BY_NAME                                       'transpose'
          1        SEND_VAL_EX                                              'F%23sus7%2FC%23'
          2        SEND_VAL_EX                                              3
          3        DO_FCALL                                      0  $0      
          4        ECHO                                                     $0
   59     5      > RETURN                                                   1

Function transpose:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 51
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 103
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 71
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 98, Position 2 = 99
Branch analysis from position: 98
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 99
Branch analysis from position: 71
2 jumps found. (Code = 43) Position 1 = 98, Position 2 = 99
Branch analysis from position: 98
Branch analysis from position: 99
Branch analysis from position: 103
Branch analysis from position: 51
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 51
Branch analysis from position: 50
Branch analysis from position: 51
filename:       /in/Cd9Pg
function name:  transpose
number of ops:  105
compiled vars:  !0 = $chord, !1 = $transpose, !2 = $chords, !3 = $result, !4 = $root_arr, !5 = $root, !6 = $root_extra_info, !7 = $root_index, !8 = $root_transpose_index, !9 = $non_root, !10 = $non_root_extra_info, !11 = $non_root_index, !12 = $non_root_transpose_index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        ASSIGN                                                   !2, <array>
   12     3        ASSIGN                                                   !3, ''
   15     4        INIT_FCALL                                               'explode'
          5        SEND_VAL                                                 '%2F'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $15     
          8        ASSIGN                                                   !4, $15
   16     9        INIT_FCALL                                               'strtoupper'
         10        FETCH_DIM_R                                      ~17     !4, 0
         11        SEND_VAL                                                 ~17
         12        DO_ICALL                                         $18     
         13        ASSIGN                                                   !5, $18
   19    14        FETCH_DIM_R                                      ~20     !5, 0
         15        INIT_FCALL                                               'strpos'
         16        SEND_VAR                                                 !5
         17        SEND_VAL                                                 '%23'
         18        DO_ICALL                                         $21     
         19        TYPE_CHECK                                  1018          $21
         20      > JMPZ                                                     ~22, ->23
         21    >   QM_ASSIGN                                        ~23     '%23'
         22      > JMP                                                      ->24
         23    >   QM_ASSIGN                                        ~23     ''
         24    >   CONCAT                                           ~24     ~20, ~23
         25        ASSIGN                                                   !5, ~24
   22    26        INIT_FCALL                                               'str_replace'
         27        SEND_VAL                                                 '%23'
         28        SEND_VAL                                                 ''
         29        INIT_FCALL                                               'substr'
         30        FETCH_DIM_R                                      ~26     !4, 0
         31        SEND_VAL                                                 ~26
         32        SEND_VAL                                                 1
         33        DO_ICALL                                         $27     
         34        SEND_VAR                                                 $27
         35        DO_ICALL                                         $28     
         36        ASSIGN                                                   !6, $28
   25    37        INIT_FCALL                                               'array_search'
         38        SEND_VAR                                                 !5
         39        SEND_VAR                                                 !2
         40        DO_ICALL                                         $30     
         41        ASSIGN                                                   !7, $30
   27    42        INIT_FCALL                                               'floor'
         43        ADD                                              ~32     !7, !1
         44        MOD                                              ~33     ~32, 12
         45        SEND_VAL                                                 ~33
         46        DO_ICALL                                         $34     
         47        ASSIGN                                                   !8, $34
   29    48        IS_SMALLER                                               !8, 0
         49      > JMPZ                                                     ~36, ->51
   31    50    >   ASSIGN_OP                                     1          !8, 12
   34    51    >   FETCH_DIM_R                                      ~38     !2, !8
         52        CONCAT                                           ~39     ~38, !6
         53        ASSIGN_OP                                     8          !3, ~39
   36    54        COUNT                                            ~41     !4
         55        IS_SMALLER                                               1, ~41
         56      > JMPZ                                                     ~42, ->103
   39    57    >   FETCH_DIM_R                                      ~43     !4, 1
         58        ASSIGN                                                   !9, ~43
   41    59        INIT_FCALL                                               'strtoupper'
         60        FETCH_DIM_R                                      ~45     !9, 0
         61        SEND_VAL                                                 ~45
         62        DO_ICALL                                         $46     
         63        INIT_FCALL                                               'strpos'
         64        SEND_VAR                                                 !9
         65        SEND_VAL                                                 '%23'
         66        DO_ICALL                                         $47     
         67        TYPE_CHECK                                  1018          $47
         68      > JMPZ                                                     ~48, ->71
         69    >   QM_ASSIGN                                        ~49     '%23'
         70      > JMP                                                      ->72
         71    >   QM_ASSIGN                                        ~49     ''
         72    >   CONCAT                                           ~50     $46, ~49
         73        ASSIGN                                                   !9, ~50
   43    74        INIT_FCALL                                               'str_replace'
         75        SEND_VAL                                                 '%23'
         76        SEND_VAL                                                 ''
         77        INIT_FCALL                                               'substr'
         78        FETCH_DIM_R                                      ~52     !4, 1
         79        SEND_VAL                                                 ~52
         80        SEND_VAL                                                 1
         81        DO_ICALL                                         $53     
         82        SEND_VAR                                                 $53
         83        DO_ICALL                                         $54     
         84        ASSIGN                                                   !10, $54
   46    85        INIT_FCALL                                               'array_search'
         86        SEND_VAR                                                 !9
         87        SEND_VAR                                                 !2
         88        DO_ICALL                                         $56     
         89        ASSIGN                                                   !11, $56
   48    90        INIT_FCALL                                               'floor'
         91        ADD                                              ~58     !11, !1
         92        MOD                                              ~59     ~58, 12
         93        SEND_VAL                                                 ~59
         94        DO_ICALL                                         $60     
         95        ASSIGN                                                   !12, $60
   50    96        IS_SMALLER                                               !12, 0
         97      > JMPZ                                                     ~62, ->99
   52    98    >   ASSIGN_OP                                     1          !12, 12
   55    99    >   FETCH_DIM_R                                      ~64     !2, !12
        100        CONCAT                                           ~65     '%2F', ~64
        101        CONCAT                                           ~66     ~65, !10
        102        ASSIGN_OP                                     8          !3, ~66
   58   103    > > RETURN                                                   !3
   59   104*     > RETURN                                                   null

End of function transpose

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.39 ms | 1404 KiB | 27 Q