3v4l.org

run code in 300+ PHP versions simultaneously
<?php function BinSearch($needle, $haystack){ (int) $m = ceil(count($haystack) / 2); $midpoint = $haystack[$m]; $lower_half = array_slice($haystack, 0, $m); $upper_half = array_slice($haystack, -$m, count($haystack)); if ($needle == $midpoint){ return $haystack[$needle]; } else if ($needle < $midpoint) { return BinSearch($needle, $lower_half); } else if ($needle > $midpoint) { return BinSearch($needle, $upper_half); } } $x = range(0,9999); BinSearch(8273,$x);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WNd1u
function name:  (null)
number of ops:  10
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 0
          2        SEND_VAL                                                 9999
          3        DO_ICALL                                         $1      
          4        ASSIGN                                                   !0, $1
   26     5        INIT_FCALL                                               'binsearch'
          6        SEND_VAL                                                 8273
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0          
          9      > RETURN                                                   1

Function binsearch:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 31
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 39
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 46
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WNd1u
function name:  BinSearch
number of ops:  47
compiled vars:  !0 = $needle, !1 = $haystack, !2 = $m, !3 = $midpoint, !4 = $lower_half, !5 = $upper_half
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        INIT_FCALL                                               'ceil'
          3        COUNT                                            ~6      !1
          4        DIV                                              ~7      ~6, 2
          5        SEND_VAL                                                 ~7
          6        DO_ICALL                                         $8      
          7        ASSIGN                                           ~9      !2, $8
          8        CAST                                          4  ~10     ~9
          9        FREE                                                     ~10
    6    10        FETCH_DIM_R                                      ~11     !1, !2
         11        ASSIGN                                                   !3, ~11
    8    12        INIT_FCALL                                               'array_slice'
         13        SEND_VAR                                                 !1
         14        SEND_VAL                                                 0
         15        SEND_VAR                                                 !2
         16        DO_ICALL                                         $13     
         17        ASSIGN                                                   !4, $13
    9    18        INIT_FCALL                                               'array_slice'
         19        SEND_VAR                                                 !1
         20        MUL                                              ~15     !2, -1
         21        SEND_VAL                                                 ~15
         22        COUNT                                            ~16     !1
         23        SEND_VAL                                                 ~16
         24        DO_ICALL                                         $17     
         25        ASSIGN                                                   !5, $17
   11    26        IS_EQUAL                                                 !0, !3
         27      > JMPZ                                                     ~19, ->31
         28    >   FETCH_DIM_R                                      ~20     !1, !0
         29      > RETURN                                                   ~20
         30*       JMP                                                      ->46
   13    31    >   IS_SMALLER                                               !0, !3
         32      > JMPZ                                                     ~21, ->39
   15    33    >   INIT_FCALL_BY_NAME                                       'BinSearch'
         34        SEND_VAR_EX                                              !0
         35        SEND_VAR_EX                                              !4
         36        DO_FCALL                                      0  $22     
         37      > RETURN                                                   $22
         38*       JMP                                                      ->46
   18    39    >   IS_SMALLER                                               !3, !0
         40      > JMPZ                                                     ~23, ->46
   20    41    >   INIT_FCALL_BY_NAME                                       'BinSearch'
         42        SEND_VAR_EX                                              !0
         43        SEND_VAR_EX                                              !5
         44        DO_FCALL                                      0  $24     
         45      > RETURN                                                   $24
   23    46    > > RETURN                                                   null

End of function binsearch

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.32 ms | 1403 KiB | 20 Q