3v4l.org

run code in 300+ PHP versions simultaneously
<?php function hasLoop($head) { $slow = $head; $fast = $head; while ($fast !== null && $fast->next !== null) { $slow = $slow->next; $fast = $fast->next->next; if ($slow === $fast) { return true; } } return false; } // example usage class Node { public $data; public $next; public function __construct($data) { $this->data = $data; $this->next = null; } } $a = new Node(1); $b = new Node(2); $c = new Node(3); $d = new Node(4); $a->next = $b; $b->next = $c; $c->next = $d; var_dump(hasLoop($a)); // false $d->next = $b; var_dump(hasLoop($a)); // true
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v8UZJ
function name:  (null)
number of ops:  37
compiled vars:  !0 = $a, !1 = $b, !2 = $c, !3 = $d
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   NEW                                              $4      'Node'
          1        SEND_VAL_EX                                              1
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $4
   31     4        NEW                                              $7      'Node'
          5        SEND_VAL_EX                                              2
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $7
   32     8        NEW                                              $10     'Node'
          9        SEND_VAL_EX                                              3
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !2, $10
   33    12        NEW                                              $13     'Node'
         13        SEND_VAL_EX                                              4
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !3, $13
   35    16        ASSIGN_OBJ                                               !0, 'next'
         17        OP_DATA                                                  !1
   36    18        ASSIGN_OBJ                                               !1, 'next'
         19        OP_DATA                                                  !2
   37    20        ASSIGN_OBJ                                               !2, 'next'
         21        OP_DATA                                                  !3
   38    22        INIT_FCALL                                               'var_dump'
         23        INIT_FCALL                                               'hasloop'
         24        SEND_VAR                                                 !0
         25        DO_FCALL                                      0  $19     
         26        SEND_VAR                                                 $19
         27        DO_ICALL                                                 
   40    28        ASSIGN_OBJ                                               !3, 'next'
         29        OP_DATA                                                  !1
   41    30        INIT_FCALL                                               'var_dump'
         31        INIT_FCALL                                               'hasloop'
         32        SEND_VAR                                                 !0
         33        DO_FCALL                                      0  $22     
         34        SEND_VAR                                                 $22
         35        DO_ICALL                                                 
         36      > RETURN                                                   1

Function hasloop:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 4
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
Branch analysis from position: 17
filename:       /in/v8UZJ
function name:  hasLoop
number of ops:  20
compiled vars:  !0 = $head, !1 = $slow, !2 = $fast
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, !0
    5     2        ASSIGN                                                   !2, !0
    7     3      > JMP                                                      ->12
    8     4    >   FETCH_OBJ_R                                      ~5      !1, 'next'
          5        ASSIGN                                                   !1, ~5
    9     6        FETCH_OBJ_R                                      ~7      !2, 'next'
          7        FETCH_OBJ_R                                      ~8      ~7, 'next'
          8        ASSIGN                                                   !2, ~8
   11     9        IS_IDENTICAL                                             !1, !2
         10      > JMPZ                                                     ~10, ->12
   12    11    > > RETURN                                                   <true>
    7    12    >   TYPE_CHECK                                  1020  ~11     !2
         13      > JMPZ_EX                                          ~11     ~11, ->17
         14    >   FETCH_OBJ_R                                      ~12     !2, 'next'
         15        TYPE_CHECK                                  1020  ~13     ~12
         16        BOOL                                             ~11     ~13
         17    > > JMPNZ                                                    ~11, ->4
   16    18    > > RETURN                                                   <false>
   17    19*     > RETURN                                                   null

End of function hasloop

Class Node:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v8UZJ
function name:  __construct
number of ops:  6
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        ASSIGN_OBJ                                               'data'
          2        OP_DATA                                                  !0
   26     3        ASSIGN_OBJ                                               'next'
          4        OP_DATA                                                  null
   27     5      > RETURN                                                   null

End of function __construct

End of class Node.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.2 ms | 1017 KiB | 16 Q