3v4l.org

run code in 300+ PHP versions simultaneously
<?php function nearest(array $arr, $needle) { $closest = current($arr); foreach ($arr as $val) { if ($val === $needle) { return $val; // found it :) } if ($val > $closest && $val < $needle) { // we found something closer and less than the needle, so use it $closest = $val; } } // we've reached the end, so return the closest solution return $closest; } $array = array(2012, 2013, 2015, 2016, 2018); $needle = 2000; print nearest($array, $needle);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GHshe
function name:  (null)
number of ops:  8
compiled vars:  !0 = $array, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   ASSIGN                                                   !0, <array>
   19     1        ASSIGN                                                   !1, 2000
   21     2        INIT_FCALL                                               'nearest'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
          7      > RETURN                                                   1

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

End of function nearest

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.52 ms | 1390 KiB | 16 Q