3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Generate a random string, using a cryptographically secure * pseudorandom number generator (random_int) * * per https://stackoverflow.com/a/31107425 */ function get_random_string($length = 16, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { $str = ''; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $str .= $keyspace[random_int(0, $max)]; } return $str; } // Tests. echo 'Test 01: ' . get_random_string() . "\n"; echo get_random_string(2) . "\n"; echo get_random_string(32) . "\n"; echo get_random_string(16, '1234567') . "\n"; echo get_random_string(16, '123abcABC') . "\n"; echo get_random_string(16, 'ABC') . "\n"; echo get_random_string(16, '10') . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b5lXg
function name:  (null)
number of ops:  40
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   INIT_FCALL                                               'get_random_string'
          1        DO_FCALL                                      0  $0      
          2        CONCAT                                           ~1      'Test+01%3A+', $0
          3        CONCAT                                           ~2      ~1, '%0A'
          4        ECHO                                                     ~2
   21     5        INIT_FCALL                                               'get_random_string'
          6        SEND_VAL                                                 2
          7        DO_FCALL                                      0  $3      
          8        CONCAT                                           ~4      $3, '%0A'
          9        ECHO                                                     ~4
   22    10        INIT_FCALL                                               'get_random_string'
         11        SEND_VAL                                                 32
         12        DO_FCALL                                      0  $5      
         13        CONCAT                                           ~6      $5, '%0A'
         14        ECHO                                                     ~6
   23    15        INIT_FCALL                                               'get_random_string'
         16        SEND_VAL                                                 16
         17        SEND_VAL                                                 '1234567'
         18        DO_FCALL                                      0  $7      
         19        CONCAT                                           ~8      $7, '%0A'
         20        ECHO                                                     ~8
   24    21        INIT_FCALL                                               'get_random_string'
         22        SEND_VAL                                                 16
         23        SEND_VAL                                                 '123abcABC'
         24        DO_FCALL                                      0  $9      
         25        CONCAT                                           ~10     $9, '%0A'
         26        ECHO                                                     ~10
   25    27        INIT_FCALL                                               'get_random_string'
         28        SEND_VAL                                                 16
         29        SEND_VAL                                                 'ABC'
         30        DO_FCALL                                      0  $11     
         31        CONCAT                                           ~12     $11, '%0A'
         32        ECHO                                                     ~12
   26    33        INIT_FCALL                                               'get_random_string'
         34        SEND_VAL                                                 16
         35        SEND_VAL                                                 '10'
         36        DO_FCALL                                      0  $13     
         37        CONCAT                                           ~14     $13, '%0A'
         38        ECHO                                                     ~14
         39      > RETURN                                                   1

Function get_random_string:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 11
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 11
Branch analysis from position: 20
Branch analysis from position: 11
filename:       /in/b5lXg
function name:  get_random_string
number of ops:  22
compiled vars:  !0 = $length, !1 = $keyspace, !2 = $str, !3 = $max, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV_INIT                                        !0      16
          1        RECV_INIT                                        !1      '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
   11     2        ASSIGN                                                   !2, ''
   12     3        INIT_FCALL                                               'mb_strlen'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 '8bit'
          6        DO_ICALL                                         $6      
          7        SUB                                              ~7      $6, 1
          8        ASSIGN                                                   !3, ~7
   13     9        ASSIGN                                                   !4, 0
         10      > JMP                                                      ->18
   14    11    >   INIT_FCALL                                               'random_int'
         12        SEND_VAL                                                 0
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                         $10     
         15        FETCH_DIM_R                                      ~11     !1, $10
         16        ASSIGN_OP                                     8          !2, ~11
   13    17        PRE_INC                                                  !4
         18    >   IS_SMALLER                                               !4, !0
         19      > JMPNZ                                                    ~14, ->11
   16    20    > > RETURN                                                   !2
   17    21*     > RETURN                                                   null

End of function get_random_string

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
184.23 ms | 1403 KiB | 24 Q