3v4l.org

run code in 300+ PHP versions simultaneously
<?php $weight_distribution = [ '1' => 0, '2-5' => 0, '5-10' => 6, '10-15' => 7, '15-20' => 3, ]; function distribute_weights(int|float $weight, array &$distribution): void { $parsed = []; foreach ($distribution as $range => $cost) { if (sscanf($range, '%d-%d', $min, $max) === 1) { $default = [$min, $min]; } else { $parsed[] = [$min, $max]; } } while ($weight > 0) { foreach ($parsed as $i => [$min, $max]) { if ($weight <= $max) { if (!$i && $weight <= $min) { [$min, $max] = $default; } break; } } if ($min === $max) { ++$distribution[$max]; } else { ++$distribution[$min . '-' . $max]; } $weight -= $max; } } distribute_weights(21, $weight_distribution); print_r($weight_distribution);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dBsO8
function name:  (null)
number of ops:  9
compiled vars:  !0 = $weight_distribution
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   41     1        INIT_FCALL                                               'distribute_weights'
          2        SEND_VAL                                                 21
          3        SEND_REF                                                 !0
          4        DO_FCALL                                      0          
   43     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

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

End of function distribute_weights

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.33 ms | 1093 KiB | 16 Q