3v4l.org

run code in 300+ PHP versions simultaneously
<?php function concat_path_components($path_components) { $first_component = array_shift($path_components); if ($first_component == NULL) return ""; return array_reduce( $path_components, function ($path, $path_component) { return $path . DIRECTORY_SEPARATOR . $path_component; }, $first_component); } function path_components($path) { $components = array(); while ($path != "." && $path != "/") { array_unshift($components, basename($path)); $path = dirname($path); } if ($path == "/") array_unshift($components, "/"); return $components; } function path_relative_to($path, $rel) { $path_components = path_components($path); $copy = $path_components; $rel_components = path_components($rel); for($i = 0 ; $i < count($rel_components) ; ++$i) { if ($path_components[$i] == $rel[$i]) array_shift($copy); else break; } echo $i . " " . count($rel) . PHP_EOL; if ($i == count($rel_components)) return concat_path_components($copy); else return NULL; } var_dump(path_relative_to("/a/b/c", "/a/b"));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I6WFv
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'path_relative_to'
          2        SEND_VAL                                                 '%2Fa%2Fb%2Fc'
          3        SEND_VAL                                                 '%2Fa%2Fb'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function concat_path_components:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I6WFv
function name:  concat_path_components
number of ops:  16
compiled vars:  !0 = $path_components, !1 = $first_component
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'array_shift'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $2      
          4        ASSIGN                                                   !1, $2
    7     5        IS_EQUAL                                                 !1, null
          6      > JMPZ                                                     ~4, ->8
    8     7    > > RETURN                                                   ''
   10     8    >   INIT_FCALL                                               'array_reduce'
   11     9        SEND_VAR                                                 !0
   12    10        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FI6WFv%3A12%240'
   15    11        SEND_VAL                                                 ~5
   16    12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $6      
         14      > RETURN                                                   $6
   17    15*     > RETURN                                                   null

End of function concat_path_components

Function %00%7Bclosure%7D%2Fin%2FI6WFv%3A12%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/I6WFv
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $path, !1 = $path_component
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        CONCAT                                           ~2      !0, '%2F'
          3        CONCAT                                           ~3      ~2, !1
          4      > RETURN                                                   ~3
   15     5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FI6WFv%3A12%240

Function path_components:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 46) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 3
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Branch analysis from position: 3
2 jumps found. (Code = 46) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
Branch analysis from position: 18
Branch analysis from position: 18
filename:       /in/I6WFv
function name:  path_components
number of ops:  27
compiled vars:  !0 = $path, !1 = $components
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   21     1        ASSIGN                                                   !1, <array>
   22     2      > JMP                                                      ->14
   24     3    >   INIT_FCALL                                               'array_unshift'
          4        SEND_REF                                                 !1
          5        INIT_FCALL                                               'basename'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $3      
          8        SEND_VAR                                                 $3
          9        DO_ICALL                                                 
   25    10        INIT_FCALL                                               'dirname'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $5      
         13        ASSIGN                                                   !0, $5
   22    14    >   IS_NOT_EQUAL                                     ~7      !0, '.'
         15      > JMPZ_EX                                          ~7      ~7, ->18
         16    >   IS_NOT_EQUAL                                     ~8      !0, '%2F'
         17        BOOL                                             ~7      ~8
         18    > > JMPNZ                                                    ~7, ->3
   28    19    >   IS_EQUAL                                                 !0, '%2F'
         20      > JMPZ                                                     ~9, ->25
   29    21    >   INIT_FCALL                                               'array_unshift'
         22        SEND_REF                                                 !1
         23        SEND_VAL                                                 '%2F'
         24        DO_ICALL                                                 
   31    25    > > RETURN                                                   !1
   32    26*     > RETURN                                                   null

End of function path_components

Function path_relative_to:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 13
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 39
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 13
Branch analysis from position: 26
Branch analysis from position: 13
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
filename:       /in/I6WFv
function name:  path_relative_to
number of ops:  41
compiled vars:  !0 = $path, !1 = $rel, !2 = $path_components, !3 = $copy, !4 = $rel_components, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   36     2        INIT_FCALL                                               'path_components'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $6      
          5        ASSIGN                                                   !2, $6
   37     6        ASSIGN                                                   !3, !2
   38     7        INIT_FCALL                                               'path_components'
          8        SEND_VAR                                                 !1
          9        DO_FCALL                                      0  $9      
         10        ASSIGN                                                   !4, $9
   40    11        ASSIGN                                                   !5, 0
         12      > JMP                                                      ->23
   42    13    >   FETCH_DIM_R                                      ~12     !2, !5
         14        FETCH_DIM_R                                      ~13     !1, !5
         15        IS_EQUAL                                                 ~12, ~13
         16      > JMPZ                                                     ~14, ->21
   43    17    >   INIT_FCALL                                               'array_shift'
         18        SEND_REF                                                 !3
         19        DO_ICALL                                                 
         20      > JMP                                                      ->22
   45    21    > > JMP                                                      ->26
   40    22    >   PRE_INC                                                  !5
         23    >   COUNT                                            ~17     !4
         24        IS_SMALLER                                               !5, ~17
         25      > JMPNZ                                                    ~18, ->13
   47    26    >   CONCAT                                           ~19     !5, '+'
         27        COUNT                                            ~20     !1
         28        CONCAT                                           ~21     ~19, ~20
         29        CONCAT                                           ~22     ~21, '%0A'
         30        ECHO                                                     ~22
   49    31        COUNT                                            ~23     !4
         32        IS_EQUAL                                                 !5, ~23
         33      > JMPZ                                                     ~24, ->39
   50    34    >   INIT_FCALL                                               'concat_path_components'
         35        SEND_VAR                                                 !3
         36        DO_FCALL                                      0  $25     
         37      > RETURN                                                   $25
         38*       JMP                                                      ->40
   52    39    > > RETURN                                                   null
   53    40*     > RETURN                                                   null

End of function path_relative_to

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.33 ms | 1398 KiB | 29 Q