3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getNearestMatch($number) { $arr = array_values([2, 4,4, 8, 16,16, 23]); sort($arr); $key = 0; while(isset($arr[$key+1]) && $arr[$key] < $number) $key++; if($arr[$key] == $number || !isset($arr[$key-1]) || $arr[$key] - $number < $number - $arr[$key-1]) return $arr[$key]; else return $arr[$key-1]; } //To test it we pass some values to the function foreach([0, 2, 3, 4, 8, 12, 22, 23, 24] as $v) echo getNearestMatch($v) . PHP_EOL;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 1, Position 2 = 8
Branch analysis from position: 1
2 jumps found. (Code = 78) Position 1 = 2, Position 2 = 8
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/j0Frv
function name:  (null)
number of ops:  10
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E > > FE_RESET_R                                       $1      <array>, ->8
          1    > > FE_FETCH_R                                               $1, !0, ->8
   20     2    >   INIT_FCALL                                               'getnearestmatch'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        CONCAT                                           ~3      $2, '%0A'
          6        ECHO                                                     ~3
   19     7      > JMP                                                      ->1
          8    >   FE_FREE                                                  $1
   20     9      > RETURN                                                   1

Function getnearestmatch:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 10
Branch analysis from position: 18
2 jumps found. (Code = 47) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
2 jumps found. (Code = 47) Position 1 = 26, Position 2 = 33
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 37
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 25
Branch analysis from position: 10
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
Branch analysis from position: 17
Branch analysis from position: 17
filename:       /in/j0Frv
function name:  getNearestMatch
number of ops:  41
compiled vars:  !0 = $number, !1 = $arr, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        INIT_FCALL                                               'array_values'
          2        SEND_VAL                                                 <array>
          3        DO_ICALL                                         $3      
          4        ASSIGN                                                   !1, $3
    5     5        INIT_FCALL                                               'sort'
          6        SEND_REF                                                 !1
          7        DO_ICALL                                                 
    6     8        ASSIGN                                                   !2, 0
    8     9      > JMP                                                      ->11
    9    10    >   PRE_INC                                                  !2
    8    11    >   ADD                                              ~8      !2, 1
         12        ISSET_ISEMPTY_DIM_OBJ                         0  ~9      !1, ~8
         13      > JMPZ_EX                                          ~9      ~9, ->17
         14    >   FETCH_DIM_R                                      ~10     !1, !2
         15        IS_SMALLER                                       ~11     ~10, !0
         16        BOOL                                             ~9      ~11
         17    > > JMPNZ                                                    ~9, ->10
   11    18    >   FETCH_DIM_R                                      ~12     !1, !2
         19        IS_EQUAL                                         ~13     !0, ~12
         20      > JMPNZ_EX                                         ~13     ~13, ->25
         21    >   SUB                                              ~14     !2, 1
         22        ISSET_ISEMPTY_DIM_OBJ                         0  ~15     !1, ~14
         23        BOOL_NOT                                         ~16     ~15
         24        BOOL                                             ~13     ~16
         25    > > JMPNZ_EX                                         ~13     ~13, ->33
         26    >   FETCH_DIM_R                                      ~17     !1, !2
         27        SUB                                              ~18     ~17, !0
         28        SUB                                              ~19     !2, 1
         29        FETCH_DIM_R                                      ~20     !1, ~19
         30        SUB                                              ~21     !0, ~20
         31        IS_SMALLER                                       ~22     ~18, ~21
         32        BOOL                                             ~13     ~22
         33    > > JMPZ                                                     ~13, ->37
   12    34    >   FETCH_DIM_R                                      ~23     !1, !2
         35      > RETURN                                                   ~23
         36*       JMP                                                      ->40
   14    37    >   SUB                                              ~24     !2, 1
         38        FETCH_DIM_R                                      ~25     !1, ~24
         39      > RETURN                                                   ~25
   16    40*     > RETURN                                                   null

End of function getnearestmatch

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.27 ms | 1403 KiB | 18 Q