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) { $currPos -= floor($currPos/2); $moves++; } else if ($currItem > $needle) { $currPos += ceil($currPos/2); $moves++; } } } echo searchInArray($array, 420);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Y607U
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
   30     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 = 48
Branch analysis from position: 48
2 jumps found. (Code = 46) Position 1 = 50, Position 2 = 52
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 13
Branch analysis from position: 53
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 = 40
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 48
Branch analysis from position: 42
2 jumps found. (Code = 46) Position 1 = 50, Position 2 = 52
Branch analysis from position: 50
Branch analysis from position: 52
Branch analysis from position: 48
Branch analysis from position: 52
filename:       /in/Y607U
function name:  searchInArray
number of ops:  54
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                                                      ->48
   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                                                      ->48
   19    31    >   IS_SMALLER                                               !6, !1
         32      > JMPZ                                                     ~28, ->40
   20    33    >   INIT_FCALL                                               'floor'
         34        DIV                                              ~29     !2, 2
         35        SEND_VAL                                                 ~29
         36        DO_ICALL                                         $30     
         37        ASSIGN_OP                                     2          !2, $30
   21    38        PRE_INC                                                  !5
         39      > JMP                                                      ->48
   23    40    >   IS_SMALLER                                               !1, !6
         41      > JMPZ                                                     ~33, ->48
   24    42    >   INIT_FCALL                                               'ceil'
         43        DIV                                              ~34     !2, 2
         44        SEND_VAL                                                 ~34
         45        DO_ICALL                                         $35     
         46        ASSIGN_OP                                     1          !2, $35
   25    47        PRE_INC                                                  !5
   12    48    >   IS_SMALLER_OR_EQUAL                              ~38     !4, !2
         49      > JMPZ_EX                                          ~38     ~38, ->52
         50    >   IS_SMALLER_OR_EQUAL                              ~39     !2, !3
         51        BOOL                                             ~38     ~39
         52    > > JMPNZ                                                    ~38, ->13
   28    53    > > RETURN                                                   null

End of function searchinarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.13 ms | 1394 KiB | 20 Q