3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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
Jump found. (Code = 62) Position 1 = -2
filename:       /in/KKlc3
function name:  (null)
number of ops:  15
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  15     0  E >   NOP                                                      
  25     1        INIT_FCALL                                               'var_dump'
         2        INIT_FCALL                                               'random_str'
         3        SEND_VAL                                                 32
         4        DO_FCALL                                      0  $0      
         5        SEND_VAR                                                 $0
         6        DO_ICALL                                                 
  26     7        INIT_FCALL                                               'var_dump'
         8        INIT_FCALL                                               'random_str'
         9        SEND_VAL                                                 8
        10        SEND_VAL                                                 'abcdefghijklmnopqrstuvwxyz'
        11        DO_FCALL                                      0  $2      
        12        SEND_VAR                                                 $2
        13        DO_ICALL                                                 
        14      > RETURN                                                   1

Function random_str:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Jump found. (Code = 44) Position 1 = 20, Position 2 = 11
Branch analysis from position: 20
Jump found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
Jump 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_CONCAT                                 0          !2, $11
  19    17        PRE_INC                                                  !4
        18    >   IS_SMALLER                                       ~14     !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 7.1.0