3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = range(1,100000); //100k items, ordered. function searchInArray(array $haystack, $needle) { $needle = (int)$needle; $currPos = (int)(count($haystack) / 2); $topBoundry = count($haystack); $bottomBoundry = 0; $moves = 0; while ($currPos >= $bottomBoundry && $currPos <= $topBoundry) { $currItem = $haystack[$currPos]; echo "Move: $moves. Current: $currPos:$currItem\n"; if ($currItem == $needle) { echo "Found item in $moves moves!"; return $currPos; } else if ($currItem > $needle) { $topBoundry = $currPos; $currPos -= floor(($currPos+$bottomBoundry)/2); $moves++; } else if ($currItem < $needle) { $bottomBoundry = $currPos; $currPos += ceil(($currPos+$topBoundry)/2); $moves++; } } } echo searchInArray($array, 420);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T74A6
function name:  (null)
number of ops:  11
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 1
          2        SEND_VAL                                                 100000
          3        DO_ICALL                                         $1      
          4        ASSIGN                                                   !0, $1
   32     5        INIT_FCALL                                               'searchinarray'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 420
          8        DO_FCALL                                      0  $3      
          9        ECHO                                                     $3
         10      > RETURN                                                   1

Function searchinarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 46) Position 1 = 54, Position 2 = 56
Branch analysis from position: 54
2 jumps found. (Code = 44) Position 1 = 57, Position 2 = 13
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 31
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 42
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 52
Branch analysis from position: 44
2 jumps found. (Code = 46) Position 1 = 54, Position 2 = 56
Branch analysis from position: 54
Branch analysis from position: 56
Branch analysis from position: 52
Branch analysis from position: 56
filename:       /in/T74A6
function name:  searchInArray
number of ops:  58
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $currPos, !3 = $topBoundry, !4 = $bottomBoundry, !5 = $moves, !6 = $currItem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        CAST                                          4  ~7      !1
          3        ASSIGN                                                   !1, ~7
    7     4        COUNT                                            ~9      !0
          5        DIV                                              ~10     ~9, 2
          6        CAST                                          4  ~11     ~10
          7        ASSIGN                                                   !2, ~11
    8     8        COUNT                                            ~13     !0
          9        ASSIGN                                                   !3, ~13
    9    10        ASSIGN                                                   !4, 0
   10    11        ASSIGN                                                   !5, 0
   12    12      > JMP                                                      ->52
   13    13    >   FETCH_DIM_R                                      ~17     !0, !2
         14        ASSIGN                                                   !6, ~17
   14    15        ROPE_INIT                                     7  ~20     'Move%3A+'
         16        ROPE_ADD                                      1  ~20     ~20, !5
         17        ROPE_ADD                                      2  ~20     ~20, '.+Current%3A+'
         18        ROPE_ADD                                      3  ~20     ~20, !2
         19        ROPE_ADD                                      4  ~20     ~20, '%3A'
         20        ROPE_ADD                                      5  ~20     ~20, !6
         21        ROPE_END                                      6  ~19     ~20, '%0A'
         22        ECHO                                                     ~19
   15    23        IS_EQUAL                                                 !6, !1
         24      > JMPZ                                                     ~24, ->31
   16    25    >   ROPE_INIT                                     3  ~26     'Found+item+in+'
         26        ROPE_ADD                                      1  ~26     ~26, !5
         27        ROPE_END                                      2  ~25     ~26, '+moves%21'
         28        ECHO                                                     ~25
   17    29      > RETURN                                                   !2
         30*       JMP                                                      ->52
   19    31    >   IS_SMALLER                                               !1, !6
         32      > JMPZ                                                     ~28, ->42
   20    33    >   ASSIGN                                                   !3, !2
   21    34        INIT_FCALL                                               'floor'
         35        ADD                                              ~30     !2, !4
         36        DIV                                              ~31     ~30, 2
         37        SEND_VAL                                                 ~31
         38        DO_ICALL                                         $32     
         39        ASSIGN_OP                                     2          !2, $32
   22    40        PRE_INC                                                  !5
         41      > JMP                                                      ->52
   24    42    >   IS_SMALLER                                               !6, !1
         43      > JMPZ                                                     ~35, ->52
   25    44    >   ASSIGN                                                   !4, !2
   26    45        INIT_FCALL                                               'ceil'
         46        ADD                                              ~37     !2, !3
         47        DIV                                              ~38     ~37, 2
         48        SEND_VAL                                                 ~38
         49        DO_ICALL                                         $39     
         50        ASSIGN_OP                                     1          !2, $39
   27    51        PRE_INC                                                  !5
   12    52    >   IS_SMALLER_OR_EQUAL                              ~42     !4, !2
         53      > JMPZ_EX                                          ~42     ~42, ->56
         54    >   IS_SMALLER_OR_EQUAL                              ~43     !2, !3
         55        BOOL                                             ~42     ~43
         56    > > JMPNZ                                                    ~42, ->13
   30    57    > > RETURN                                                   null

End of function searchinarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.36 ms | 1403 KiB | 20 Q