3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Generate a random string, using a cryptographically secure * pseudorandom number generator (random_int) * * For PHP 7, random_int is a PHP core function * For PHP 5.x, depends on https://github.com/paragonie/random_compat * * @param int $length How many characters do we want? * @param string $keyspace A string of all possible characters * to select from * @return string */ function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { $str = ''; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $str .= $keyspace[random_int(0, $max)]; } return $str; } var_dump(random_str(32)); var_dump(random_str(8, 'abcdefghijklmnopqrstuvwxyz'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KKlc3
function name:  (null)
number of ops:  14
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'random_str'
          2        SEND_VAL                                                 32
          3        DO_FCALL                                      0  $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
   26     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL                                               'random_str'
          8        SEND_VAL                                                 8
          9        SEND_VAL                                                 'abcdefghijklmnopqrstuvwxyz'
         10        DO_FCALL                                      0  $2      
         11        SEND_VAR                                                 $2
         12        DO_ICALL                                                 
         13      > RETURN                                                   1

Function random_str:
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/KKlc3
function name:  random_str
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
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
   17     2        ASSIGN                                                   !2, ''
   18     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
   19     9        ASSIGN                                                   !4, 0
         10      > JMP                                                      ->18
   20    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
   19    17        PRE_INC                                                  !4
         18    >   IS_SMALLER                                               !4, !0
         19      > JMPNZ                                                    ~14, ->11
   22    20    > > RETURN                                                   !2
   23    21*     > RETURN                                                   null

End of function random_str

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
178.1 ms | 1403 KiB | 21 Q