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 Searching from $bottomBoundry to $topBoundry\n"; if ($currItem == $needle) { echo "Found item in $moves moves!"; return $currPos; } else if ($currItem > $needle) { echo "LARGER! Let's go smaller! "; $topBoundry = $currPos; $currPos = floor(($currPos+$bottomBoundry)/2); $moves++; } else if ($currItem < $needle) { echo "SMALLER! Let's go larger! "; $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/VUldj
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
   34     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 = 58
Branch analysis from position: 58
2 jumps found. (Code = 46) Position 1 = 60, Position 2 = 62
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 63, Position 2 = 13
Branch analysis from position: 63
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 47
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 58
Branch analysis from position: 49
2 jumps found. (Code = 46) Position 1 = 60, Position 2 = 62
Branch analysis from position: 60
Branch analysis from position: 62
Branch analysis from position: 58
Branch analysis from position: 62
filename:       /in/VUldj
function name:  searchInArray
number of ops:  64
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                                                      ->58
   13    13    >   FETCH_DIM_R                                      ~17     !0, !2
         14        ASSIGN                                                   !6, ~17
   14    15        ROPE_INIT                                    11  ~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_ADD                                      6  ~20     ~20, '+Searching+from+'
         22        ROPE_ADD                                      7  ~20     ~20, !4
         23        ROPE_ADD                                      8  ~20     ~20, '+to+'
         24        ROPE_ADD                                      9  ~20     ~20, !3
         25        ROPE_END                                     10  ~19     ~20, '%0A'
         26        ECHO                                                     ~19
   15    27        IS_EQUAL                                                 !6, !1
         28      > JMPZ                                                     ~26, ->35
   16    29    >   ROPE_INIT                                     3  ~28     'Found+item+in+'
         30        ROPE_ADD                                      1  ~28     ~28, !5
         31        ROPE_END                                      2  ~27     ~28, '+moves%21'
         32        ECHO                                                     ~27
   17    33      > RETURN                                                   !2
         34*       JMP                                                      ->58
   19    35    >   IS_SMALLER                                               !1, !6
         36      > JMPZ                                                     ~30, ->47
   20    37    >   ECHO                                                     'LARGER%21+Let%27s+go+smaller%21+'
   21    38        ASSIGN                                                   !3, !2
   22    39        INIT_FCALL                                               'floor'
         40        ADD                                              ~32     !2, !4
         41        DIV                                              ~33     ~32, 2
         42        SEND_VAL                                                 ~33
         43        DO_ICALL                                         $34     
         44        ASSIGN                                                   !2, $34
   23    45        PRE_INC                                                  !5
         46      > JMP                                                      ->58
   25    47    >   IS_SMALLER                                               !6, !1
         48      > JMPZ                                                     ~37, ->58
   26    49    >   ECHO                                                     'SMALLER%21+Let%27s+go+larger%21+'
   27    50        ASSIGN                                                   !4, !2
   28    51        INIT_FCALL                                               'ceil'
         52        ADD                                              ~39     !2, !3
         53        DIV                                              ~40     ~39, 2
         54        SEND_VAL                                                 ~40
         55        DO_ICALL                                         $41     
         56        ASSIGN                                                   !2, $41
   29    57        PRE_INC                                                  !5
   12    58    >   IS_SMALLER_OR_EQUAL                              ~44     !4, !2
         59      > JMPZ_EX                                          ~44     ~44, ->62
         60    >   IS_SMALLER_OR_EQUAL                              ~45     !2, !3
         61        BOOL                                             ~44     ~45
         62    > > JMPNZ                                                    ~44, ->13
   32    63    > > RETURN                                                   null

End of function searchinarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.34 ms | 1394 KiB | 20 Q