3v4l.org

run code in 300+ PHP versions simultaneously
<?php $rules = [ '1' => '1.2', '5-10' => '6.25', '10-15' => '9.2', '15-20' => '10.9', ]; function costFromWeight($weight, array $newRules): array { $parsed = []; foreach ($newRules as $range => $cost) { if (sscanf($range, '%d-%d', $min, $max) === 1) { $default = [$min, $min, (float) $cost]; } else { $parsed[] = [$min, $max, (float) $cost]; } } $result = ['cost' => 0]; while ($weight > 0) { foreach ($parsed as $i => [$min, $max, $cost]) { if ($weight <= $max) { if (!$i && $weight <= $min) { [$min, $max, $cost] = $default; } break; } } $result[$max] = ($result[$max] ?? 0) + 1; $result['cost'] += $cost; $weight -= $max; } return $result; } foreach ([49, 47.75, 38, 35, 23, 15, 14, 10.5, 4] as $weight) { echo "$weight :: "; print_r(costFromWeight($weight, $rules)); echo "\n---\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 15
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/2BJsO
function name:  (null)
number of ops:  17
compiled vars:  !0 = $rules, !1 = $weight
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   38     1      > FE_RESET_R                                       $3      <array>, ->15
          2    > > FE_FETCH_R                                               $3, !1, ->15
   39     3    >   NOP                                                      
          4        FAST_CONCAT                                      ~4      !1, '+%3A%3A+'
          5        ECHO                                                     ~4
   40     6        INIT_FCALL                                               'print_r'
          7        INIT_FCALL                                               'costfromweight'
          8        SEND_VAR                                                 !1
          9        SEND_VAR                                                 !0
         10        DO_FCALL                                      0  $5      
         11        SEND_VAR                                                 $5
         12        DO_ICALL                                                 
   41    13        ECHO                                                     '%0A---%0A'
   38    14      > JMP                                                      ->2
         15    >   FE_FREE                                                  $3
   42    16      > RETURN                                                   1

Function costfromweight:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 27
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 27
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 30
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
2 jumps found. (Code = 77) Position 1 = 31, Position 2 = 57
Branch analysis from position: 31
2 jumps found. (Code = 78) Position 1 = 32, Position 2 = 57
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 56
Branch analysis from position: 42
2 jumps found. (Code = 46) Position 1 = 44, Position 2 = 46
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 55
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 30
Branch analysis from position: 69
Branch analysis from position: 30
Branch analysis from position: 55
Branch analysis from position: 46
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 57
Branch analysis from position: 57
Branch analysis from position: 27
filename:       /in/2BJsO
function name:  costFromWeight
number of ops:  73
compiled vars:  !0 = $weight, !1 = $newRules, !2 = $parsed, !3 = $cost, !4 = $range, !5 = $min, !6 = $max, !7 = $default, !8 = $result, !9 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        ASSIGN                                                   !2, <array>
   12     3      > FE_RESET_R                                       $11     !1, ->27
          4    > > FE_FETCH_R                                       ~12     $11, !3, ->27
          5    >   ASSIGN                                                   !4, ~12
   13     6        INIT_FCALL                                               'sscanf'
          7        SEND_VAR                                                 !4
          8        SEND_VAL                                                 '%25d-%25d'
          9        SEND_REF                                                 !5
         10        SEND_REF                                                 !6
         11        DO_ICALL                                         $14     
         12        IS_IDENTICAL                                             $14, 1
         13      > JMPZ                                                     ~15, ->20
   14    14    >   INIT_ARRAY                                       ~16     !5
         15        ADD_ARRAY_ELEMENT                                ~16     !5
         16        CAST                                          5  ~17     !3
         17        ADD_ARRAY_ELEMENT                                ~16     ~17
         18        ASSIGN                                                   !7, ~16
   13    19      > JMP                                                      ->26
   16    20    >   INIT_ARRAY                                       ~20     !5
         21        ADD_ARRAY_ELEMENT                                ~20     !6
         22        CAST                                          5  ~21     !3
         23        ADD_ARRAY_ELEMENT                                ~20     ~21
         24        ASSIGN_DIM                                               !2
         25        OP_DATA                                                  ~20
   12    26    > > JMP                                                      ->4
         27    >   FE_FREE                                                  $11
   20    28        ASSIGN                                                   !8, <array>
   21    29      > JMP                                                      ->67
   22    30    > > FE_RESET_R                                       $23     !2, ->57
         31    > > FE_FETCH_R                                       ~31     $23, $24, ->57
         32    >   FETCH_LIST_R                                     $25     $24, 0
         33        ASSIGN                                                   !5, $25
         34        FETCH_LIST_R                                     $27     $24, 1
         35        ASSIGN                                                   !6, $27
         36        FETCH_LIST_R                                     $29     $24, 2
         37        ASSIGN                                                   !3, $29
         38        FREE                                                     $24
         39        ASSIGN                                                   !9, ~31
   23    40        IS_SMALLER_OR_EQUAL                                      !0, !6
         41      > JMPZ                                                     ~33, ->56
   24    42    >   BOOL_NOT                                         ~34     !9
         43      > JMPZ_EX                                          ~34     ~34, ->46
         44    >   IS_SMALLER_OR_EQUAL                              ~35     !0, !5
         45        BOOL                                             ~34     ~35
         46    > > JMPZ                                                     ~34, ->55
   25    47    >   QM_ASSIGN                                        ~36     !7
         48        FETCH_LIST_R                                     $37     ~36, 0
         49        ASSIGN                                                   !5, $37
         50        FETCH_LIST_R                                     $39     ~36, 1
         51        ASSIGN                                                   !6, $39
         52        FETCH_LIST_R                                     $41     ~36, 2
         53        ASSIGN                                                   !3, $41
         54        FREE                                                     ~36
   27    55    > > JMP                                                      ->57
   22    56    > > JMP                                                      ->31
         57    >   FE_FREE                                                  $23
   30    58        FETCH_DIM_IS                                     ~44     !8, !6
         59        COALESCE                                         ~45     ~44
         60        QM_ASSIGN                                        ~45     0
         61        ADD                                              ~46     ~45, 1
         62        ASSIGN_DIM                                               !8, !6
         63        OP_DATA                                                  ~46
   31    64        ASSIGN_DIM_OP                +=               1          !8, 'cost'
         65        OP_DATA                                                  !3
   32    66        ASSIGN_OP                                     2          !0, !6
   21    67    >   IS_SMALLER                                               0, !0
         68      > JMPNZ                                                    ~49, ->30
   34    69    >   VERIFY_RETURN_TYPE                                       !8
         70      > RETURN                                                   !8
   35    71*       VERIFY_RETURN_TYPE                                       
         72*     > RETURN                                                   null

End of function costfromweight

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.35 ms | 1449 KiB | 16 Q