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 'Test 02: ' . get_random_string(2) . "\n"; echo 'Test 03: ' . get_random_string(32) . "\n"; echo 'Test 04: ' . get_random_string(16, '1234567') . "\n"; echo 'Test 05: ' . get_random_string(16, '123abcABC') . "\n"; echo 'Test 06: ' . get_random_string(16, 'ABC') . "\n"; echo 'Test 07: ' . get_random_string(16, '10') . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/U0UDe
function name:  (null)
number of ops:  46
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      'Test+02%3A+', $3
          9        CONCAT                                           ~5      ~4, '%0A'
         10        ECHO                                                     ~5
   22    11        INIT_FCALL                                               'get_random_string'
         12        SEND_VAL                                                 32
         13        DO_FCALL                                      0  $6      
         14        CONCAT                                           ~7      'Test+03%3A+', $6
         15        CONCAT                                           ~8      ~7, '%0A'
         16        ECHO                                                     ~8
   23    17        INIT_FCALL                                               'get_random_string'
         18        SEND_VAL                                                 16
         19        SEND_VAL                                                 '1234567'
         20        DO_FCALL                                      0  $9      
         21        CONCAT                                           ~10     'Test+04%3A+', $9
         22        CONCAT                                           ~11     ~10, '%0A'
         23        ECHO                                                     ~11
   24    24        INIT_FCALL                                               'get_random_string'
         25        SEND_VAL                                                 16
         26        SEND_VAL                                                 '123abcABC'
         27        DO_FCALL                                      0  $12     
         28        CONCAT                                           ~13     'Test+05%3A+', $12
         29        CONCAT                                           ~14     ~13, '%0A'
         30        ECHO                                                     ~14
   25    31        INIT_FCALL                                               'get_random_string'
         32        SEND_VAL                                                 16
         33        SEND_VAL                                                 'ABC'
         34        DO_FCALL                                      0  $15     
         35        CONCAT                                           ~16     'Test+06%3A+', $15
         36        CONCAT                                           ~17     ~16, '%0A'
         37        ECHO                                                     ~17
   26    38        INIT_FCALL                                               'get_random_string'
         39        SEND_VAL                                                 16
         40        SEND_VAL                                                 '10'
         41        DO_FCALL                                      0  $18     
         42        CONCAT                                           ~19     'Test+07%3A+', $18
         43        CONCAT                                           ~20     ~19, '%0A'
         44        ECHO                                                     ~20
         45      > 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/U0UDe
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:
148.27 ms | 1402 KiB | 24 Q