3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getClosest($search, $arr) { $closest = null; foreach($arr as $item) { if($closest == null || abs($search - $closest) > abs($item - $search)) { $closest = $item; } } return $closest; } $arr = array(0,5,10,15,20); $number = 13; echo getclosest($number,$arr);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ccX08
function name:  (null)
number of ops:  8
compiled vars:  !0 = $arr, !1 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   ASSIGN                                                   !0, <array>
   14     1        ASSIGN                                                   !1, 13
   15     2        INIT_FCALL                                               'getclosest'
          3        SEND_VAR                                                 !1
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
          7      > RETURN                                                   1

Function getclosest:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 20
Branch analysis from position: 5
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 19
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 19
Branch analysis from position: 17
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/ccX08
function name:  getClosest
number of ops:  23
compiled vars:  !0 = $search, !1 = $arr, !2 = $closest, !3 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        ASSIGN                                                   !2, null
    5     3      > FE_RESET_R                                       $5      !1, ->20
          4    > > FE_FETCH_R                                               $5, !3, ->20
    6     5    >   IS_EQUAL                                         ~6      !2, null
          6      > JMPNZ_EX                                         ~6      ~6, ->17
          7    >   INIT_FCALL                                               'abs'
          8        SUB                                              ~7      !0, !2
          9        SEND_VAL                                                 ~7
         10        DO_ICALL                                         $8      
         11        INIT_FCALL                                               'abs'
         12        SUB                                              ~9      !3, !0
         13        SEND_VAL                                                 ~9
         14        DO_ICALL                                         $10     
         15        IS_SMALLER                                       ~11     $10, $8
         16        BOOL                                             ~6      ~11
         17    > > JMPZ                                                     ~6, ->19
    7    18    >   ASSIGN                                                   !2, !3
    5    19    > > JMP                                                      ->4
         20    >   FE_FREE                                                  $5
   10    21      > RETURN                                                   !2
   11    22*     > RETURN                                                   null

End of function getclosest

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.83 ms | 1390 KiB | 16 Q