3v4l.org

run code in 300+ PHP versions simultaneously
<?php $i = 0; function is_cyclic(array &$array) { global $i; // Exit when too deep. if (++$i > 10) exit; echo "Call $i:\n"; var_dump($array); echo "\n"; $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)) { //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(); is_cyclic($bomb);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dSbjC
function name:  (null)
number of ops:  8
compiled vars:  !0 = $i, !1 = $bomb
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 0
   45     1        INIT_FCALL                                               'craft_bomb'
          2        DO_FCALL                                      0  $3      
          3        ASSIGN                                                   !1, $3
   46     4        INIT_FCALL                                               'is_cyclic'
          5        SEND_REF                                                 !1
          6        DO_FCALL                                      0          
          7      > RETURN                                                   1

Function is_cyclic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 27
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 31
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
2 jumps found. (Code = 125) Position 1 = 34, Position 2 = 45
Branch analysis from position: 34
2 jumps found. (Code = 126) Position 1 = 35, Position 2 = 45
Branch analysis from position: 35
2 jumps found. (Code = 46) Position 1 = 37, Position 2 = 41
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 44
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 41
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
Branch analysis from position: 27
filename:       /in/dSbjC
function name:  is_cyclic
number of ops:  48
compiled vars:  !0 = $array, !1 = $i, !2 = $lastKey, !3 = $marker, !4 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    7     1        BIND_GLOBAL                                              !1, 'i'
   10     2        PRE_INC                                          ~5      !1
          3        IS_SMALLER                                               10, ~5
          4      > JMPZ                                                     ~6, ->6
          5    > > EXIT                                                     
   12     6    >   ROPE_INIT                                     3  ~8      'Call+'
          7        ROPE_ADD                                      1  ~8      ~8, !1
          8        ROPE_END                                      2  ~7      ~8, '%3A%0A'
          9        ECHO                                                     ~7
   13    10        INIT_FCALL                                               'var_dump'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                                 
   14    13        ECHO                                                     '%0A'
   16    14        INIT_FCALL                                               'array_key_last'
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $11     
         17        ASSIGN                                                   !2, $11
   17    18        TYPE_CHECK                                    2          !2
         19      > JMPZ                                                     ~13, ->21
   19    20    > > RETURN                                                   <false>
   21    21    >   BIND_STATIC                                              !3
   22    22        TYPE_CHECK                                    2          !3
         23      > JMPZ                                                     ~14, ->27
   23    24    >   NEW                                              $15     'stdClass'
         25        DO_FCALL                                      0          
         26        ASSIGN                                                   !3, $15
   25    27    >   FETCH_DIM_R                                      ~18     !0, !2
         28        IS_IDENTICAL                                             !3, ~18
         29      > JMPZ                                                     ~19, ->31
   26    30    > > RETURN                                                   <true>
   28    31    >   ASSIGN_DIM                                               !0
         32        OP_DATA                                                  !3
   29    33      > FE_RESET_RW                                      $21     !0, ->45
         34    > > FE_FETCH_RW                                              $21, !4, ->45
   30    35    >   TYPE_CHECK                                  128  ~22     !4
         36      > JMPZ_EX                                          ~22     ~22, ->41
         37    >   INIT_FCALL_BY_NAME                                       'is_cyclic'
         38        SEND_VAR_EX                                              !4
         39        DO_FCALL                                      0  $23     
         40        BOOL                                             ~22     $23
         41    > > JMPZ                                                     ~22, ->44
   32    42    >   FE_FREE                                                  $21
         43      > RETURN                                                   <true>
   29    44    > > JMP                                                      ->34
         45    >   FE_FREE                                                  $21
   36    46      > RETURN                                                   <false>
   37    47*     > 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/dSbjC
function name:  craft_bomb
number of ops:  6
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   ASSIGN                                                   !0, <array>
   41     1        FETCH_DIM_W                                      $2      !0, 1
          2        FETCH_DIM_W                                      $3      $2, 1
          3        ASSIGN_REF                                               $3, !0
   42     4      > RETURN                                                   !0
   43     5*     > RETURN                                                   null

End of function craft_bomb

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.63 ms | 943 KiB | 20 Q