3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_rand_cryptographically_secure(array $array): int|string { $max = count($array) - 1; if ($max < 0) { throw new ValueError ('Argument #1 ($array) cannot be empty'); } return key(array_slice($array, random_int(0, $max), 1, true)); } $tests = [ [5, 6, 7], ['a' => 1, 'b' => 2, 'c' => 3], ['zero', 4 => 'four', 9 => 'nine'] ]; foreach ($tests as $test) { echo array_rand_cryptographically_secure($test) . "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 9
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/UHAfJ
function name:  (null)
number of ops:  11
compiled vars:  !0 = $tests, !1 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   ASSIGN                                                   !0, <array>
   16     1      > FE_RESET_R                                       $3      !0, ->9
          2    > > FE_FETCH_R                                               $3, !1, ->9
   17     3    >   INIT_FCALL                                               'array_rand_cryptographically_secure'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        CONCAT                                           ~5      $4, '%0A'
          7        ECHO                                                     ~5
   16     8      > JMP                                                      ->2
          9    >   FE_FREE                                                  $3
   18    10      > RETURN                                                   1

Function array_rand_cryptographically_secure:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UHAfJ
function name:  array_rand_cryptographically_secure
number of ops:  27
compiled vars:  !0 = $array, !1 = $max
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        COUNT                                            ~2      !0
          2        SUB                                              ~3      ~2, 1
          3        ASSIGN                                                   !1, ~3
    5     4        IS_SMALLER                                               !1, 0
          5      > JMPZ                                                     ~5, ->10
    6     6    >   NEW                                              $6      'ValueError'
          7        SEND_VAL_EX                                              'Argument+%231+%28%24array%29+cannot+be+empty'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $6
    8    10    >   INIT_FCALL                                               'key'
         11        INIT_FCALL                                               'array_slice'
         12        SEND_VAR                                                 !0
         13        INIT_FCALL                                               'random_int'
         14        SEND_VAL                                                 0
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                         $8      
         17        SEND_VAR                                                 $8
         18        SEND_VAL                                                 1
         19        SEND_VAL                                                 <true>
         20        DO_ICALL                                         $9      
         21        SEND_VAR                                                 $9
         22        DO_ICALL                                         $10     
         23        VERIFY_RETURN_TYPE                                       $10
         24      > RETURN                                                   $10
    9    25*       VERIFY_RETURN_TYPE                                       
         26*     > RETURN                                                   null

End of function array_rand_cryptographically_secure

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.49 ms | 1403 KiB | 20 Q