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( int $length = 64, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ): string { if ($length < 1) { throw new \RangeException("Length must be a positive integer"); } $pieces = []; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $pieces []= $keyspace[random_int(0, $max)]; } return implode('', $pieces); } $a = random_str(32); $b = random_str(8, 'abcdefghijklmnopqrstuvwxyz'); $c = random_str(); var_dump($a, $b, $c);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IMJGF
function name:  (null)
number of ops:  18
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   INIT_FCALL                                               'random_str'
          1        SEND_VAL                                                 32
          2        DO_FCALL                                      0  $3      
          3        ASSIGN                                                   !0, $3
   31     4        INIT_FCALL                                               'random_str'
          5        SEND_VAL                                                 8
          6        SEND_VAL                                                 'abcdefghijklmnopqrstuvwxyz'
          7        DO_FCALL                                      0  $5      
          8        ASSIGN                                                   !1, $5
   32     9        INIT_FCALL                                               'random_str'
         10        DO_FCALL                                      0  $7      
         11        ASSIGN                                                   !2, $7
   33    12        INIT_FCALL                                               'var_dump'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !1
         15        SEND_VAR                                                 !2
         16        DO_ICALL                                                 
         17      > RETURN                                                   1

Function random_str:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 17
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 17
Branch analysis from position: 27
Branch analysis from position: 17
filename:       /in/IMJGF
function name:  random_str
number of ops:  35
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_INIT                                        !0      64
          1        RECV_INIT                                        !1      '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
   19     2        IS_SMALLER                                               !0, 1
          3      > JMPZ                                                     ~5, ->8
   20     4    >   NEW                                              $6      'RangeException'
          5        SEND_VAL_EX                                              'Length+must+be+a+positive+integer'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $6
   22     8    >   ASSIGN                                                   !2, <array>
   23     9        INIT_FCALL                                               'mb_strlen'
         10        SEND_VAR                                                 !1
         11        SEND_VAL                                                 '8bit'
         12        DO_ICALL                                         $9      
         13        SUB                                              ~10     $9, 1
         14        ASSIGN                                                   !3, ~10
   24    15        ASSIGN                                                   !4, 0
         16      > JMP                                                      ->25
   25    17    >   INIT_FCALL                                               'random_int'
         18        SEND_VAL                                                 0
         19        SEND_VAR                                                 !3
         20        DO_ICALL                                         $14     
         21        FETCH_DIM_R                                      ~15     !1, $14
         22        ASSIGN_DIM                                               !2
         23        OP_DATA                                                  ~15
   24    24        PRE_INC                                                  !4
         25    >   IS_SMALLER                                               !4, !0
         26      > JMPNZ                                                    ~17, ->17
   27    27    >   INIT_FCALL                                               'implode'
         28        SEND_VAL                                                 ''
         29        SEND_VAR                                                 !2
         30        DO_ICALL                                         $18     
         31        VERIFY_RETURN_TYPE                                       $18
         32      > RETURN                                                   $18
   28    33*       VERIFY_RETURN_TYPE                                       
         34*     > RETURN                                                   null

End of function random_str

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.13 ms | 1454 KiB | 20 Q