3v4l.org

run code in 300+ PHP versions simultaneously
<?php $prices = array( array('id' => '28','price' =>100 ), array('id' => '29','price' =>105 ), array('id' => '30','price' =>110 ), array('id' => '31','price' =>115 ), array('id' => '32','price' =>120 ), array('id' => '33','price' =>125 ), array('id' => '34','price' =>130 ) ); function getNearestHighest($prices, $amount){ $resullt = null; $diff = PHP_INT_MAX; foreach($prices as $p){ if($p['price'] >= $amount){ if(abs($p['price'] - $amount) < $diff){ $result = $p; $diff = abs($p['price'] - $amount); } } } if(is_null($result)) throw new \Exception("No nearest highest amount exists!"); return $result; } try{ print_r(getNearestHighest($prices, 113)); }catch(\Exception $e){ // whatever you wish to do }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 9
Branch analysis from position: 9
2 jumps found. (Code = 107) Position 1 = 10, Position 2 = -2
Branch analysis from position: 10
filename:       /in/25ViW
function name:  (null)
number of ops:  11
compiled vars:  !0 = $prices, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   31     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'getnearesthighest'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 113
          5        DO_FCALL                                      0  $3      
          6        SEND_VAR                                                 $3
          7        DO_ICALL                                                 
          8      > JMP                                                      ->10
   32     9  E > > CATCH                                       last         'Exception'
   34    10    > > RETURN                                                   1

Function getnearesthighest:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 24
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 24
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 23
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 23
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 23
Branch analysis from position: 23
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/25ViW
function name:  getNearestHighest
number of ops:  33
compiled vars:  !0 = $prices, !1 = $amount, !2 = $resullt, !3 = $diff, !4 = $p, !5 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        ASSIGN                                                   !2, null
   15     3        ASSIGN                                                   !3, 9223372036854775807
   17     4      > FE_RESET_R                                       $8      !0, ->24
          5    > > FE_FETCH_R                                               $8, !4, ->24
   18     6    >   FETCH_DIM_R                                      ~9      !4, 'price'
          7        IS_SMALLER_OR_EQUAL                                      !1, ~9
          8      > JMPZ                                                     ~10, ->23
   19     9    >   INIT_FCALL                                               'abs'
         10        FETCH_DIM_R                                      ~11     !4, 'price'
         11        SUB                                              ~12     ~11, !1
         12        SEND_VAL                                                 ~12
         13        DO_ICALL                                         $13     
         14        IS_SMALLER                                               $13, !3
         15      > JMPZ                                                     ~14, ->23
   20    16    >   ASSIGN                                                   !5, !4
   21    17        INIT_FCALL                                               'abs'
         18        FETCH_DIM_R                                      ~16     !4, 'price'
         19        SUB                                              ~17     ~16, !1
         20        SEND_VAL                                                 ~17
         21        DO_ICALL                                         $18     
         22        ASSIGN                                                   !3, $18
   17    23    > > JMP                                                      ->5
         24    >   FE_FREE                                                  $8
   26    25        TYPE_CHECK                                    2          !5
         26      > JMPZ                                                     ~20, ->31
         27    >   NEW                                              $21     'Exception'
         28        SEND_VAL_EX                                              'No+nearest+highest+amount+exists%21'
         29        DO_FCALL                                      0          
         30      > THROW                                         0          $21
   27    31    > > RETURN                                                   !5
   28    32*     > RETURN                                                   null

End of function getnearesthighest

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
218.33 ms | 1013 KiB | 16 Q