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 "Move: $moves. Current: $currPos:$currItem"; } } echo searchInArray($array, 420);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qGrM5
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
   29     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 = 46
Branch analysis from position: 46
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 50
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 51, Position 2 = 13
Branch analysis from position: 51
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
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 50
Branch analysis from position: 48
Branch analysis from position: 50
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 = 48, Position 2 = 50
Branch analysis from position: 48
Branch analysis from position: 50
Branch analysis from position: 39
Branch analysis from position: 50
filename:       /in/qGrM5
function name:  searchInArray
number of ops:  52
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                                                      ->46
   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
   25    39    >   ROPE_INIT                                     6  ~33     'Move%3A+'
         40        ROPE_ADD                                      1  ~33     ~33, !5
         41        ROPE_ADD                                      2  ~33     ~33, '.+Current%3A+'
         42        ROPE_ADD                                      3  ~33     ~33, !2
         43        ROPE_ADD                                      4  ~33     ~33, '%3A'
         44        ROPE_END                                      5  ~32     ~33, !6
         45        ECHO                                                     ~32
   12    46    >   IS_SMALLER_OR_EQUAL                              ~36     !4, !2
         47      > JMPZ_EX                                          ~36     ~36, ->50
         48    >   IS_SMALLER_OR_EQUAL                              ~37     !2, !3
         49        BOOL                                             ~36     ~37
         50    > > JMPNZ                                                    ~36, ->13
   27    51    > > RETURN                                                   null

End of function searchinarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
205.35 ms | 1407 KiB | 20 Q