3v4l.org

run code in 300+ 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 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 42
Branch analysis from position: 38
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 45
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 45
Branch analysis from position: 55
Branch analysis from position: 45
filename:       /in/hd3hL
function name:  random_str
number of ops:  57
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        INIT_FCALL                                               'is_numeric'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $7      
          5        BOOL_NOT                                         ~8      $7
          6      > JMPZ                                                     ~8, ->11
   15     7    >   NEW                                              $9      'InvalidArgumentException'
   16     8        SEND_VAL_EX                                              'random_str+-+Argument+1+-+expected+an+integer'
   15     9        DO_FCALL                                      0          
   16    10      > THROW                                         0          $9
   19    11    >   TYPE_CHECK                                   64  ~11     !1
         12        BOOL_NOT                                         ~12     ~11
         13      > JMPZ                                                     ~12, ->18
   20    14    >   NEW                                              $13     'InvalidArgumentException'
   21    15        SEND_VAL_EX                                              'random_str+-+Argument+2+-+expected+a+string'
   20    16        DO_FCALL                                      0          
   21    17      > THROW                                         0          $13
   25    18    >   IS_SMALLER                                               !0, 1
         19      > JMPZ                                                     ~15, ->21
   27    20    > > RETURN                                                   ''
   31    21    >   INIT_FCALL                                               'str_split'
         22        SEND_VAR                                                 !1
         23        DO_ICALL                                         $16     
         24        ASSIGN                                                   !2, $16
   32    25        INIT_FCALL                                               'implode'
         26        SEND_VAL                                                 ''
         27        INIT_FCALL                                               'array_unique'
         28        SEND_VAR                                                 !2
         29        DO_ICALL                                         $18     
         30        SEND_VAR                                                 $18
         31        DO_ICALL                                         $19     
         32        ASSIGN                                                   !1, $19
   35    33        STRLEN                                           ~21     !1
         34        SUB                                              ~22     ~21, 1
         35        ASSIGN                                                   !3, ~22
   36    36        IS_SMALLER                                               !3, 1
         37      > JMPZ                                                     ~24, ->42
   38    38    >   NEW                                              $25     'LogicException'
   39    39        SEND_VAL_EX                                              'random_str+-+Argument+2+-+expected+a+string+that+contains+at+least+2+distinct+characters'
   38    40        DO_FCALL                                      0          
   39    41      > THROW                                         0          $25
   43    42    >   ASSIGN                                                   !4, ''
   44    43        ASSIGN                                                   !5, 0
         44      > JMP                                                      ->53
   45    45    >   INIT_FCALL                                               'random_int'
         46        SEND_VAL                                                 0
         47        SEND_VAR                                                 !3
         48        DO_ICALL                                         $29     
         49        ASSIGN                                                   !6, $29
   46    50        FETCH_DIM_R                                      ~31     !1, !6
         51        ASSIGN_OP                                     8          !4, ~31
   44    52        PRE_INC                                                  !5
         53    >   IS_SMALLER                                               !5, !0
         54      > JMPNZ                                                    ~34, ->45
   48    55    > > RETURN                                                   !4
   49    56*     > RETURN                                                   null

End of function random_str

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.9 ms | 1470 KiB | 23 Q