3v4l.org

run code in 500+ PHP versions simultaneously
<?php /** * Generate a random string * * @link https://paragonie.com/b/JvICXzh_jhLyt4y3 * * @param int $length - How long should our random string be? * @param string $charset - A string of all possible characters to choose from * @return string */ function random_str($length = 32, $charset = 'abcdefghijklmnopqrstuvwxyz') { // Type checks: if (!is_numeric($length)) { throw new InvalidArgumentException( 'random_str - Argument 1 - expected an integer' ); } if (!is_string($charset)) { throw new InvalidArgumentException( 'random_str - Argument 2 - expected a string' ); } if ($length < 1) { // Just return an empty string. Any value < 1 is meaningless. return ''; } // Remove duplicate characters from $charset $split = str_split($charset); $charset = implode('', array_unique($split)); // This is the maximum index for all of the characters in the string $charset $charset_max = strlen($charset) - 1; if ($charset_max < 1) { // Avoid letting users do: random_str($int, 'a'); -> 'aaaaa...' throw new LogicException( 'random_str - Argument 2 - expected a string that contains at least 2 distinct characters' ); } // Now that we have good data, this is the meat of our function: $random_str = ''; for ($i = 0; $i < $length; ++$i) { $r = random_int(0, $charset_max); $random_str .= $charset[$r]; } return $random_str; } /* ------------------------------------------------------------------------ */ var_dump(random_str(10)); var_dump(random_str(16, '0123456789')); var_dump(random_str(8, '0123456789abcdef')); var_dump(random_str(24, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hd3hL
function name:  (null)
number of ops:  28
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   52     0  E >   INIT_FCALL                                                   'var_dump'
          1        INIT_FCALL                                                   'random_str'
          2        SEND_VAL                                                     10
          3        DO_FCALL                                          0  $0      
          4        SEND_VAR                                                     $0
          5        DO_ICALL                                                     
   54     6        INIT_FCALL                                                   'var_dump'
          7        INIT_FCALL                                                   'random_str'
          8        SEND_VAL                                                     16
          9        SEND_VAL                                                     '0123456789'
         10        DO_FCALL                                          0  $2      
         11        SEND_VAR                                                     $2
         12        DO_ICALL                                                     
   56    13        INIT_FCALL                                                   'var_dump'
         14        INIT_FCALL                                                   'random_str'
         15        SEND_VAL                                                     8
         16        SEND_VAL                                                     '0123456789abcdef'
         17        DO_FCALL                                          0  $4      
         18        SEND_VAR                                                     $4
         19        DO_ICALL                                                     
   58    20        INIT_FCALL                                                   'var_dump'
         21        INIT_FCALL                                                   'random_str'
         22        SEND_VAL                                                     24
         23        SEND_VAL                                                     'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B.'
         24        DO_FCALL                                          0  $6      
         25        SEND_VAR                                                     $6
         26        DO_ICALL                                                     
         27      > RETURN                                                       1

Function random_str:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 19
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 37
Branch analysis from position: 33
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 40
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 40
Branch analysis from position: 50
Branch analysis from position: 40
filename:       /in/hd3hL
function name:  random_str
number of ops:  52
compiled vars:  !0 = $length, !1 = $charset, !2 = $split, !3 = $charset_max, !4 = $random_str, !5 = $i, !6 = $r
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   11     0  E >   RECV_INIT                                            !0      32
          1        RECV_INIT                                            !1      'abcdefghijklmnopqrstuvwxyz'
   14     2        FRAMELESS_ICALL_1                is_numeric          ~7      !0
          3        BOOL_NOT                                             ~8      ~7
          4      > JMPZ                                                         ~8, ->9
   15     5    >   NEW                                                  $9      'InvalidArgumentException'
   16     6        SEND_VAL_EX                                                  'random_str+-+Argument+1+-+expected+an+integer'
   15     7        DO_FCALL                                          0          
   16     8      > THROW                                             0          $9
   19     9    >   TYPE_CHECK                                       64  ~11     !1
         10        BOOL_NOT                                             ~12     ~11
         11      > JMPZ                                                         ~12, ->16
   20    12    >   NEW                                                  $13     'InvalidArgumentException'
   21    13        SEND_VAL_EX                                                  'random_str+-+Argument+2+-+expected+a+string'
   20    14        DO_FCALL                                          0          
   21    15      > THROW                                             0          $13
   25    16    >   IS_SMALLER                                                   !0, 1
         17      > JMPZ                                                         ~15, ->19
   27    18    > > RETURN                                                       ''
   31    19    >   INIT_FCALL                                                   'str_split'
         20        SEND_VAR                                                     !1
         21        DO_ICALL                                             $16     
         22        ASSIGN                                                       !2, $16
   32    23        INIT_FCALL                                                   'array_unique'
         24        SEND_VAR                                                     !2
         25        DO_ICALL                                             $18     
         26        FRAMELESS_ICALL_2                implode             ~19     '', $18
         27        ASSIGN                                                       !1, ~19
   35    28        STRLEN                                               ~21     !1
         29        SUB                                                  ~22     ~21, 1
         30        ASSIGN                                                       !3, ~22
   36    31        IS_SMALLER                                                   !3, 1
         32      > JMPZ                                                         ~24, ->37
   38    33    >   NEW                                                  $25     'LogicException'
   39    34        SEND_VAL_EX                                                  'random_str+-+Argument+2+-+expected+a+string+that+contains+at+least+2+distinct+characters'
   38    35        DO_FCALL                                          0          
   39    36      > THROW                                             0          $25
   43    37    >   ASSIGN                                                       !4, ''
   44    38        ASSIGN                                                       !5, 0
         39      > JMP                                                          ->48
   45    40    >   INIT_FCALL                                                   'random_int'
         41        SEND_VAL                                                     0
         42        SEND_VAR                                                     !3
         43        DO_ICALL                                             $29     
         44        ASSIGN                                                       !6, $29
   46    45        FETCH_DIM_R                                          ~31     !1, !6
         46        ASSIGN_OP                                         8          !4, ~31
   44    47        PRE_INC                                                      !5
         48    >   IS_SMALLER                                                   !5, !0
         49      > JMPNZ                                                        ~34, ->40
   48    50    > > RETURN                                                       !4
   49    51*     > RETURN                                                       null

End of function random_str

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
165.4 ms | 2460 KiB | 21 Q