3v4l.org

run code in 300+ PHP versions simultaneously
<?php function hyphenatedRanges($numbers): string { $result = []; foreach ($numbers as $i => $number) { if (isset($ref) && $number === $numbers[$i - 1] + 1) { $ref = strtok($ref, "-") . "-$number"; } else { unset($ref); $ref = $number; $result[] = &$ref; } } return implode(',', $result); } echo hyphenatedRanges([1, 2, 3, 6, 8, 9]) . "\n"; // "1-3,6,8-9" echo hyphenatedRanges([2, 4, 5, 6, 8, 10]); // "2,4-6,8,10"
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1Gjul
function name:  (null)
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   INIT_FCALL                                               'hyphenatedranges'
          1        SEND_VAL                                                 <array>
          2        DO_FCALL                                      0  $0      
          3        CONCAT                                           ~1      $0, '%0A'
          4        ECHO                                                     ~1
   20     5        INIT_FCALL                                               'hyphenatedranges'
          6        SEND_VAL                                                 <array>
          7        DO_FCALL                                      0  $2      
          8        ECHO                                                     $2
          9      > RETURN                                                   1

Function hyphenatedranges:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 27
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 27
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 22
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/1Gjul
function name:  hyphenatedRanges
number of ops:  36
compiled vars:  !0 = $numbers, !1 = $result, !2 = $number, !3 = $i, !4 = $ref
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    6     1        ASSIGN                                                   !1, <array>
    7     2      > FE_RESET_R                                       $6      !0, ->27
          3    > > FE_FETCH_R                                       ~7      $6, !2, ->27
          4    >   ASSIGN                                                   !3, ~7
    8     5        ISSET_ISEMPTY_CV                                 ~9      !4
          6      > JMPZ_EX                                          ~9      ~9, ->12
          7    >   SUB                                              ~10     !3, 1
          8        FETCH_DIM_R                                      ~11     !0, ~10
          9        ADD                                              ~12     ~11, 1
         10        IS_IDENTICAL                                     ~13     !2, ~12
         11        BOOL                                             ~9      ~13
         12    > > JMPZ                                                     ~9, ->22
    9    13    >   INIT_FCALL                                               'strtok'
         14        SEND_VAR                                                 !4
         15        SEND_VAL                                                 '-'
         16        DO_ICALL                                         $14     
         17        NOP                                                      
         18        FAST_CONCAT                                      ~15     '-', !2
         19        CONCAT                                           ~16     $14, ~15
         20        ASSIGN                                                   !4, ~16
    8    21      > JMP                                                      ->26
   11    22    >   UNSET_CV                                                 !4
   12    23        ASSIGN                                                   !4, !2
   13    24        FETCH_DIM_W                                      $19     !1
         25        ASSIGN_REF                                               $19, !4
    7    26    > > JMP                                                      ->3
         27    >   FE_FREE                                                  $6
   16    28        INIT_FCALL                                               'implode'
         29        SEND_VAL                                                 '%2C'
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                         $21     
         32        VERIFY_RETURN_TYPE                                       $21
         33      > RETURN                                                   $21
   17    34*       VERIFY_RETURN_TYPE                                       
         35*     > RETURN                                                   null

End of function hyphenatedranges

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.32 ms | 1010 KiB | 17 Q