3v4l.org

run code in 300+ PHP versions simultaneously
<?php function random_index(array $source) { $max = count($source) - 1; $r = random_int(0, $max); $k = array_keys($source); return $k[$r]; } $array = [ 'apple' => 1234, 'boy' => 2345, 'cat' => 3456, 'dog' => 4567, 'echo' => 5678, 'fortune' => 6789 ]; $i = random_index($array); var_dump([$i, $array[$i]]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1joB1
function name:  (null)
number of ops:  12
compiled vars:  !0 = $array, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   ASSIGN                                                   !0, <array>
   18     1        INIT_FCALL                                               'random_index'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   19     5        INIT_FCALL                                               'var_dump'
          6        INIT_ARRAY                                       ~5      !1
          7        FETCH_DIM_R                                      ~6      !0, !1
          8        ADD_ARRAY_ELEMENT                                ~5      ~6
          9        SEND_VAL                                                 ~5
         10        DO_ICALL                                                 
         11      > RETURN                                                   1

Function random_index:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1joB1
function name:  random_index
number of ops:  16
compiled vars:  !0 = $source, !1 = $max, !2 = $r, !3 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    4     1        COUNT                                            ~4      !0
          2        SUB                                              ~5      ~4, 1
          3        ASSIGN                                                   !1, ~5
    5     4        INIT_FCALL                                               'random_int'
          5        SEND_VAL                                                 0
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !2, $7
    6     9        INIT_FCALL                                               'array_keys'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $9      
         12        ASSIGN                                                   !3, $9
    7    13        FETCH_DIM_R                                      ~11     !3, !2
         14      > RETURN                                                   ~11
    8    15*     > RETURN                                                   null

End of function random_index

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.97 ms | 1465 KiB | 17 Q