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') { $pieces = []; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $pieces []= $keyspace[random_int(0, $max)]; } return implode('', $pieces); } 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/b4PST
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 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 11
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 11
Branch analysis from position: 21
Branch analysis from position: 11
filename:       /in/b4PST
function name:  random_str
number of ops:  27
compiled vars:  !0 = $length, !1 = $keyspace, !2 = $pieces, !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, <array>
   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                                                      ->19
   20    11    >   INIT_FCALL                                               'random_int'
         12        SEND_VAL                                                 0
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                         $11     
         15        FETCH_DIM_R                                      ~12     !1, $11
         16        ASSIGN_DIM                                               !2
         17        OP_DATA                                                  ~12
   19    18        PRE_INC                                                  !4
         19    >   IS_SMALLER                                               !4, !0
         20      > JMPNZ                                                    ~14, ->11
   22    21    >   INIT_FCALL                                               'implode'
         22        SEND_VAL                                                 ''
         23        SEND_VAR                                                 !2
         24        DO_ICALL                                         $15     
         25      > RETURN                                                   $15
   23    26*     > RETURN                                                   null

End of function random_str

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
130.07 ms | 1464 KiB | 19 Q