3v4l.org

run code in 300+ PHP versions simultaneously
<?php function is_cyclic(array &$array, int $maxDepth = -1) { if ($maxDepth === 0) { throw new Exception('Maximum nesting level exceeded.'); } $lastKey = array_key_last($array); if ($lastKey === null) { // Array is empty return false; } static $marker; if ($marker === null) { $marker = new stdClass(); } if ($array[$lastKey] === $marker) { return true; } $array[] = $marker; foreach ($array as &$item) { if (is_array($item) && is_cyclic($item, $maxDepth - 1)) { array_pop($array); return true; } } array_pop($array); return false; } function craft_bomb() { $array = [1, [2, 3]]; $array[1][1] = &$array; return $array; } $bomb = craft_bomb(); var_export(is_cyclic($bomb, 10));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Nl7PZ
function name:  (null)
number of ops:  11
compiled vars:  !0 = $bomb
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   INIT_FCALL                                               'craft_bomb'
          1        DO_FCALL                                      0  $1      
          2        ASSIGN                                                   !0, $1
   39     3        INIT_FCALL                                               'var_export'
          4        INIT_FCALL                                               'is_cyclic'
          5        SEND_REF                                                 !0
          6        SEND_VAL                                                 10
          7        DO_FCALL                                      0  $3      
          8        SEND_VAR                                                 $3
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Function is_cyclic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 25
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 125) Position 1 = 28, Position 2 = 44
Branch analysis from position: 28
2 jumps found. (Code = 126) Position 1 = 29, Position 2 = 44
Branch analysis from position: 29
2 jumps found. (Code = 46) Position 1 = 31, Position 2 = 37
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 43
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 37
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
Branch analysis from position: 21
filename:       /in/Nl7PZ
function name:  is_cyclic
number of ops:  50
compiled vars:  !0 = $array, !1 = $maxDepth, !2 = $lastKey, !3 = $marker, !4 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      -1
    5     2        IS_IDENTICAL                                             !1, 0
          3      > JMPZ                                                     ~5, ->8
    6     4    >   NEW                                              $6      'Exception'
          5        SEND_VAL_EX                                              'Maximum+nesting+level+exceeded.'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $6
    9     8    >   INIT_FCALL                                               'array_key_last'
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                         $8      
         11        ASSIGN                                                   !2, $8
   10    12        TYPE_CHECK                                    2          !2
         13      > JMPZ                                                     ~10, ->15
   12    14    > > RETURN                                                   <false>
   14    15    >   BIND_STATIC                                              !3
   15    16        TYPE_CHECK                                    2          !3
         17      > JMPZ                                                     ~11, ->21
   16    18    >   NEW                                              $12     'stdClass'
         19        DO_FCALL                                      0          
         20        ASSIGN                                                   !3, $12
   18    21    >   FETCH_DIM_R                                      ~15     !0, !2
         22        IS_IDENTICAL                                             !3, ~15
         23      > JMPZ                                                     ~16, ->25
   19    24    > > RETURN                                                   <true>
   21    25    >   ASSIGN_DIM                                               !0
         26        OP_DATA                                                  !3
   22    27      > FE_RESET_RW                                      $18     !0, ->44
         28    > > FE_FETCH_RW                                              $18, !4, ->44
   23    29    >   TYPE_CHECK                                  128  ~19     !4
         30      > JMPZ_EX                                          ~19     ~19, ->37
         31    >   INIT_FCALL_BY_NAME                                       'is_cyclic'
         32        SEND_VAR_EX                                              !4
         33        SUB                                              ~20     !1, 1
         34        SEND_VAL_EX                                              ~20
         35        DO_FCALL                                      0  $21     
         36        BOOL                                             ~19     $21
         37    > > JMPZ                                                     ~19, ->43
   24    38    >   INIT_FCALL                                               'array_pop'
         39        SEND_REF                                                 !0
         40        DO_ICALL                                                 
   25    41        FE_FREE                                                  $18
         42      > RETURN                                                   <true>
   22    43    > > JMP                                                      ->28
         44    >   FE_FREE                                                  $18
   28    45        INIT_FCALL                                               'array_pop'
         46        SEND_REF                                                 !0
         47        DO_ICALL                                                 
   29    48      > RETURN                                                   <false>
   30    49*     > RETURN                                                   null

End of function is_cyclic

Function craft_bomb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Nl7PZ
function name:  craft_bomb
number of ops:  6
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   ASSIGN                                                   !0, <array>
   34     1        FETCH_DIM_W                                      $2      !0, 1
          2        FETCH_DIM_W                                      $3      $2, 1
          3        ASSIGN_REF                                               $3, !0
   35     4      > RETURN                                                   !0
   36     5*     > RETURN                                                   null

End of function craft_bomb

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.11 ms | 1455 KiB | 18 Q