3v4l.org

run code in 300+ PHP versions simultaneously
<?php function binary_search(array $array, int $target) { $max = count($array) - 1; $min = 0; while ($max - $min > 1) { if ($target < $array[($max - $min) / 2]) { $max = ($max + $min) / 2; } else { $min = ($max + $min) / 2; } } return $result = $array[$max + $min / 2] === $target ? true : false; } echo('Searching for 6 in 1,2,3,4,5,6'); $array = [1,2,3,4,5,6]; $target = 6; $result = binary_search($array, $target); echo($result); echo('Searching for 3 in 1,2,3,4,5'); $array = [1,2,3,4,5]; $target = 3; $result = binary_search($array, $target); echo($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EXt1H
function name:  (null)
number of ops:  19
compiled vars:  !0 = $array, !1 = $target, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   ECHO                                                     'Searching+for+6+in+1%2C2%2C3%2C4%2C5%2C6'
   18     1        ASSIGN                                                   !0, <array>
   19     2        ASSIGN                                                   !1, 6
   20     3        INIT_FCALL                                               'binary_search'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !2, $5
   21     8        ECHO                                                     !2
   23     9        ECHO                                                     'Searching+for+3+in+1%2C2%2C3%2C4%2C5'
   24    10        ASSIGN                                                   !0, <array>
   25    11        ASSIGN                                                   !1, 3
   26    12        INIT_FCALL                                               'binary_search'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !1
         15        DO_FCALL                                      0  $9      
         16        ASSIGN                                                   !2, $9
   27    17        ECHO                                                     !2
         18      > RETURN                                                   1

Function binary_search:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 7
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 29
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 7
Branch analysis from position: 22
Branch analysis from position: 7
filename:       /in/EXt1H
function name:  binary_search
number of ops:  33
compiled vars:  !0 = $array, !1 = $target, !2 = $max, !3 = $min, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        COUNT                                            ~5      !0
          3        SUB                                              ~6      ~5, 1
          4        ASSIGN                                                   !2, ~6
    5     5        ASSIGN                                                   !3, 0
    7     6      > JMP                                                      ->19
    8     7    >   SUB                                              ~9      !2, !3
          8        DIV                                              ~10     ~9, 2
          9        FETCH_DIM_R                                      ~11     !0, ~10
         10        IS_SMALLER                                               !1, ~11
         11      > JMPZ                                                     ~12, ->16
    9    12    >   ADD                                              ~13     !2, !3
         13        DIV                                              ~14     ~13, 2
         14        ASSIGN                                                   !2, ~14
         15      > JMP                                                      ->19
   11    16    >   ADD                                              ~16     !2, !3
         17        DIV                                              ~17     ~16, 2
         18        ASSIGN                                                   !3, ~17
    7    19    >   SUB                                              ~19     !2, !3
         20        IS_SMALLER                                               1, ~19
         21      > JMPNZ                                                    ~20, ->7
   14    22    >   DIV                                              ~21     !3, 2
         23        ADD                                              ~22     !2, ~21
         24        FETCH_DIM_R                                      ~23     !0, ~22
         25        IS_IDENTICAL                                             !1, ~23
         26      > JMPZ                                                     ~24, ->29
         27    >   QM_ASSIGN                                        ~25     <true>
         28      > JMP                                                      ->30
         29    >   QM_ASSIGN                                        ~25     <false>
         30    >   ASSIGN                                           ~26     !4, ~25
         31      > RETURN                                                   ~26
   15    32*     > RETURN                                                   null

End of function binary_search

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.14 ms | 1402 KiB | 15 Q