3v4l.org

run code in 200+ 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);
based on 4rY4C
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:  19
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  15     0  E >   NOP                                                      
  30     1        INIT_FCALL                                               'random_str'
         2        SEND_VAL                                                 32
         3        DO_FCALL                                      0  $3      
         4        ASSIGN                                                   !0, $3
  31     5        INIT_FCALL                                               'random_str'
         6        SEND_VAL                                                 8
         7        SEND_VAL                                                 'abcdefghijklmnopqrstuvwxyz'
         8        DO_FCALL                                      0  $5      
         9        ASSIGN                                                   !1, $5
  32    10        INIT_FCALL                                               'random_str'
        11        DO_FCALL                                      0  $7      
        12        ASSIGN                                                   !2, $7
  33    13        INIT_FCALL                                               'var_dump'
        14        SEND_VAR                                                 !0
        15        SEND_VAR                                                 !1
        16        SEND_VAR                                                 !2
        17        DO_ICALL                                                 
        18      > 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                                       ~5      !0, 1
         3      > JMPZ                                                     ~5, ->8
  20     4    >   NEW                                              $6      :60
         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                                       ~17     !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 7.3.0