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]; if ($currItem == $needle) { echo "Found item in $moves moves!"; return $currPos; } else if ($currItem < $needle) { $currPos -= floor($currPos/2); $moves++; } else if ($currItem > $needle) { $currPos += ceil($currPos/2); } } } echo searchInArray($array, 420);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rmBgp
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
   28     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 = 39
Branch analysis from position: 39
2 jumps found. (Code = 46) Position 1 = 41, Position 2 = 43
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 13
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 23
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 32
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 39
Branch analysis from position: 34
2 jumps found. (Code = 46) Position 1 = 41, Position 2 = 43
Branch analysis from position: 41
Branch analysis from position: 43
Branch analysis from position: 39
Branch analysis from position: 43
filename:       /in/rmBgp
function name:  searchInArray
number of ops:  45
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                                                      ->39
   13    13    >   FETCH_DIM_R                                      ~17     !0, !2
         14        ASSIGN                                                   !6, ~17
   14    15        IS_EQUAL                                                 !6, !1
         16      > JMPZ                                                     ~19, ->23
   15    17    >   ROPE_INIT                                     3  ~21     'Found+item+in+'
         18        ROPE_ADD                                      1  ~21     ~21, !5
         19        ROPE_END                                      2  ~20     ~21, '+moves%21'
         20        ECHO                                                     ~20
   16    21      > RETURN                                                   !2
         22*       JMP                                                      ->39
   18    23    >   IS_SMALLER                                               !6, !1
         24      > JMPZ                                                     ~23, ->32
   19    25    >   INIT_FCALL                                               'floor'
         26        DIV                                              ~24     !2, 2
         27        SEND_VAL                                                 ~24
         28        DO_ICALL                                         $25     
         29        ASSIGN_OP                                     2          !2, $25
   20    30        PRE_INC                                                  !5
         31      > JMP                                                      ->39
   22    32    >   IS_SMALLER                                               !1, !6
         33      > JMPZ                                                     ~28, ->39
   23    34    >   INIT_FCALL                                               'ceil'
         35        DIV                                              ~29     !2, 2
         36        SEND_VAL                                                 ~29
         37        DO_ICALL                                         $30     
         38        ASSIGN_OP                                     1          !2, $30
   12    39    >   IS_SMALLER_OR_EQUAL                              ~32     !4, !2
         40      > JMPZ_EX                                          ~32     ~32, ->43
         41    >   IS_SMALLER_OR_EQUAL                              ~33     !2, !3
         42        BOOL                                             ~32     ~33
         43    > > JMPNZ                                                    ~32, ->13
   26    44    > > RETURN                                                   null

End of function searchinarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.72 ms | 1403 KiB | 20 Q