3v4l.org

run code in 300+ PHP versions simultaneously
<?php // id => weight $ads = [ 10 => 5, 20 => 10, 30 => 15 ]; function show_ad($ads) { // calculate the total of all weights $combined = array_sum($ads); // pick a random number from $combined weights $random = rand(0, $combined - 1); // keep subtracting weight until we drop below an ad weight foreach ($ads as $id => $weight) { if ($random < $weight) return $id; $random -= $weight; } return $random; } function simulate_views($ads, $rounds = 100) { $result = []; for ($i = 0; $i < $rounds; $i++) { $id = show_ad($ads); $result[$id] = isset($result[$id]) ? $result[$id]+1: 1; } return $result; } print_r(simulate_views($ads));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1Giuc
function name:  (null)
number of ops:  8
compiled vars:  !0 = $ads
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   37     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'simulate_views'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function show_ad:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/1Giuc
function name:  show_ad
number of ops:  23
compiled vars:  !0 = $ads, !1 = $combined, !2 = $random, !3 = $weight, !4 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   11     1        INIT_FCALL                                               'array_sum'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !1, $5
   14     5        INIT_FCALL                                               'rand'
          6        SEND_VAL                                                 0
          7        SUB                                              ~7      !1, 1
          8        SEND_VAL                                                 ~7
          9        DO_ICALL                                         $8      
         10        ASSIGN                                                   !2, $8
   17    11      > FE_RESET_R                                       $10     !0, ->20
         12    > > FE_FETCH_R                                       ~11     $10, !3, ->20
         13    >   ASSIGN                                                   !4, ~11
   18    14        IS_SMALLER                                               !2, !3
         15      > JMPZ                                                     ~13, ->18
         16    >   FE_FREE                                                  $10
         17      > RETURN                                                   !4
   20    18    >   ASSIGN_OP                                     2          !2, !3
   17    19      > JMP                                                      ->12
         20    >   FE_FREE                                                  $10
   23    21      > RETURN                                                   !2
   24    22*     > RETURN                                                   null

End of function show_ad

Function simulate_views:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 5
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 5
Branch analysis from position: 21
Branch analysis from position: 5
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 5
Branch analysis from position: 21
Branch analysis from position: 5
filename:       /in/1Giuc
function name:  simulate_views
number of ops:  23
compiled vars:  !0 = $ads, !1 = $rounds, !2 = $result, !3 = $i, !4 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      100
   27     2        ASSIGN                                                   !2, <array>
   29     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->19
   30     5    >   INIT_FCALL                                               'show_ad'
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0  $7      
          8        ASSIGN                                                   !4, $7
   32     9        ISSET_ISEMPTY_DIM_OBJ                         0          !2, !4
         10      > JMPZ                                                     ~10, ->15
         11    >   FETCH_DIM_R                                      ~11     !2, !4
         12        ADD                                              ~12     ~11, 1
         13        QM_ASSIGN                                        ~13     ~12
         14      > JMP                                                      ->16
         15    >   QM_ASSIGN                                        ~13     1
         16    >   ASSIGN_DIM                                               !2, !4
         17        OP_DATA                                                  ~13
   29    18        PRE_INC                                                  !3
         19    >   IS_SMALLER                                               !3, !1
         20      > JMPNZ                                                    ~15, ->5
   34    21    > > RETURN                                                   !2
   35    22*     > RETURN                                                   null

End of function simulate_views

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.44 ms | 1009 KiB | 18 Q