3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getClosestIdByAmount($amount = 1){ $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 ) ); $result_id = null; $take_next = false; foreach ($prices as $i => $item) { // amount < any price of the array if ($i === 0 && $item['price'] > $amount){ $result_id = $item['id']; break; } else { if ($item['price'] < $amount) { // amount > price1 AND amount < price2 $take_next = true; } elseif ($item['price'] == $amount){ // amount == some price $result_id = $item['id']; break; } else { if ($take_next){ $result_id = $item['id']; } break; } } } return "Amount: " . $amount . " Closest ID: " . $result_id . PHP_EOL; } $amount = 99; echo getClosestIdByAmount($amount); $amount = 113; echo getClosestIdByAmount($amount); $amount = 115; echo getClosestIdByAmount($amount); $amount = 131; echo getClosestIdByAmount($amount);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XPDoc
function name:  (null)
number of ops:  21
compiled vars:  !0 = $amount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   ASSIGN                                                   !0, 99
   46     1        INIT_FCALL                                               'getclosestidbyamount'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $2      
          4        ECHO                                                     $2
   47     5        ASSIGN                                                   !0, 113
   48     6        INIT_FCALL                                               'getclosestidbyamount'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $4      
          9        ECHO                                                     $4
   49    10        ASSIGN                                                   !0, 115
   50    11        INIT_FCALL                                               'getclosestidbyamount'
         12        SEND_VAR                                                 !0
         13        DO_FCALL                                      0  $6      
         14        ECHO                                                     $6
   51    15        ASSIGN                                                   !0, 131
   52    16        INIT_FCALL                                               'getclosestidbyamount'
         17        SEND_VAR                                                 !0
         18        DO_FCALL                                      0  $8      
         19        ECHO                                                     $8
         20      > RETURN                                                   1

Function getclosestidbyamount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 34
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 34
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 29
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 32
Branch analysis from position: 12
Branch analysis from position: 34
Branch analysis from position: 34
filename:       /in/XPDoc
function name:  getClosestIdByAmount
number of ops:  41
compiled vars:  !0 = $amount, !1 = $prices, !2 = $result_id, !3 = $take_next, !4 = $item, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV_INIT                                        !0      1
    5     1        ASSIGN                                                   !1, <array>
   15     2        ASSIGN                                                   !2, null
   16     3        ASSIGN                                                   !3, <false>
   18     4      > FE_RESET_R                                       $9      !1, ->34
          5    > > FE_FETCH_R                                       ~10     $9, !4, ->34
          6    >   ASSIGN                                                   !5, ~10
   21     7        IS_IDENTICAL                                     ~12     !5, 0
          8      > JMPZ_EX                                          ~12     ~12, ->12
          9    >   FETCH_DIM_R                                      ~13     !4, 'price'
         10        IS_SMALLER                                       ~14     !0, ~13
         11        BOOL                                             ~12     ~14
         12    > > JMPZ                                                     ~12, ->17
   22    13    >   FETCH_DIM_R                                      ~15     !4, 'id'
         14        ASSIGN                                                   !2, ~15
   23    15      > JMP                                                      ->34
   21    16*       JMP                                                      ->33
   26    17    >   FETCH_DIM_R                                      ~17     !4, 'price'
         18        IS_SMALLER                                               ~17, !0
         19      > JMPZ                                                     ~18, ->22
   28    20    >   ASSIGN                                                   !3, <true>
   26    21      > JMP                                                      ->33
   29    22    >   FETCH_DIM_R                                      ~20     !4, 'price'
         23        IS_EQUAL                                                 !0, ~20
         24      > JMPZ                                                     ~21, ->29
   31    25    >   FETCH_DIM_R                                      ~22     !4, 'id'
         26        ASSIGN                                                   !2, ~22
   32    27      > JMP                                                      ->34
   29    28*       JMP                                                      ->33
   34    29    > > JMPZ                                                     !3, ->32
   35    30    >   FETCH_DIM_R                                      ~24     !4, 'id'
         31        ASSIGN                                                   !2, ~24
   37    32    > > JMP                                                      ->34
   18    33    > > JMP                                                      ->5
         34    >   FE_FREE                                                  $9
   42    35        CONCAT                                           ~26     'Amount%3A+', !0
         36        CONCAT                                           ~27     ~26, '+Closest+ID%3A+'
         37        CONCAT                                           ~28     ~27, !2
         38        CONCAT                                           ~29     ~28, '%0A'
         39      > RETURN                                                   ~29
   43    40*     > RETURN                                                   null

End of function getclosestidbyamount

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
200.07 ms | 1011 KiB | 17 Q