3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [.1, .2, .7]; function weightedSelector($input, $value){ foreach ($input as $key => $value){ $total = array_sum($input); $previous = array_sum(array_slice($input, 0, $key)); $percentage = $total - $previous; print_r($total." ".$previous." ".$percentage); if ($value <= $percentage){ return $key; } } } foreach ([.1, .15, .2, .29, .3, .4, .5, .8, .99, .66] as $value){ print_r(weightedSelector($input, $value)." : ".$value); // print_r("\n"); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 13
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/mvp2H
function name:  (null)
number of ops:  15
compiled vars:  !0 = $input, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   20     1      > FE_RESET_R                                       $3      <array>, ->13
          2    > > FE_FETCH_R                                               $3, !1, ->13
   21     3    >   INIT_FCALL                                               'print_r'
          4        INIT_FCALL                                               'weightedselector'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $4      
          8        CONCAT                                           ~5      $4, '+%3A+'
          9        CONCAT                                           ~6      ~5, !1
         10        SEND_VAL                                                 ~6
         11        DO_ICALL                                                 
   20    12      > JMP                                                      ->2
         13    >   FE_FREE                                                  $3
   23    14      > RETURN                                                   1

Function weightedselector:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 32
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 32
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/mvp2H
function name:  weightedSelector
number of ops:  34
compiled vars:  !0 = $input, !1 = $value, !2 = $key, !3 = $total, !4 = $previous, !5 = $percentage
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2      > FE_RESET_R                                       $6      !0, ->32
          3    > > FE_FETCH_R                                       ~7      $6, !1, ->32
          4    >   ASSIGN                                                   !2, ~7
    7     5        INIT_FCALL                                               'array_sum'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $9      
          8        ASSIGN                                                   !3, $9
    8     9        INIT_FCALL                                               'array_sum'
         10        INIT_FCALL                                               'array_slice'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 0
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $11     
         15        SEND_VAR                                                 $11
         16        DO_ICALL                                         $12     
         17        ASSIGN                                                   !4, $12
   10    18        SUB                                              ~14     !3, !4
         19        ASSIGN                                                   !5, ~14
   12    20        INIT_FCALL                                               'print_r'
         21        CONCAT                                           ~16     !3, '+'
         22        CONCAT                                           ~17     ~16, !4
         23        CONCAT                                           ~18     ~17, '+'
         24        CONCAT                                           ~19     ~18, !5
         25        SEND_VAL                                                 ~19
         26        DO_ICALL                                                 
   14    27        IS_SMALLER_OR_EQUAL                                      !1, !5
         28      > JMPZ                                                     ~21, ->31
   15    29    >   FE_FREE                                                  $6
         30      > RETURN                                                   !2
    6    31    > > JMP                                                      ->3
         32    >   FE_FREE                                                  $6
   18    33      > RETURN                                                   null

End of function weightedselector

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.93 ms | 1403 KiB | 20 Q