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 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') { $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(3)); var_dump(random_str(3));
based on aqYEj
Output for 7.3.0rc3
string(3) "5LZ" string(3) "AJW"
Output for 7.3.0rc2
string(3) "QCM" string(3) "DFP"
Output for 7.3.0rc1
string(3) "QDE" string(3) "DIG"
Output for 7.3.0beta3
string(3) "9JY" string(3) "WM7"
Output for 7.3.0beta2
string(3) "Z2D" string(3) "M8O"
Output for 7.3.0beta1
string(3) "CZI" string(3) "XGK"
Output for 7.3.0alpha4
string(3) "YZQ" string(3) "KY1"
Output for 7.3.0alpha3
string(3) "Y1X" string(3) "2FV"
Output for 7.3.0alpha2
string(3) "IR2" string(3) "PWU"
Output for 7.3.0alpha1
string(3) "D6R" string(3) "QBY"
Output for 7.2.11
string(3) "A21" string(3) "W3Y"
Output for 7.2.10
string(3) "OD7" string(3) "D5P"
Output for 7.2.9
string(3) "3DO" string(3) "Q83"
Output for 7.2.8
string(3) "776" string(3) "8DO"
Output for 7.2.7
string(3) "HYO" string(3) "NWJ"
Output for 7.2.6
string(3) "M5C" string(3) "8LA"
Output for 7.2.5
string(3) "RKU" string(3) "K0Z"
Output for 7.2.4
string(3) "TE0" string(3) "S4L"
Output for 7.2.3
string(3) "LYQ" string(3) "8EF"
Output for 7.2.2
string(3) "N82" string(3) "EFH"
Output for 7.2.1
string(3) "JAF" string(3) "81X"
Output for 7.2.0
string(3) "6AY" string(3) "LHI"
Output for 7.1.23
string(3) "O2K" string(3) "W88"
Output for 7.1.22
string(3) "L5E" string(3) "3ZI"
Output for 7.1.21
string(3) "NPG" string(3) "YI3"
Output for 7.1.20
string(3) "4TR" string(3) "FHE"
Output for 7.1.19
string(3) "99Y" string(3) "MIP"
Output for 7.1.18
string(3) "GVD" string(3) "52A"
Output for 7.1.17
string(3) "RUN" string(3) "2GI"
Output for 7.1.16
string(3) "IHS" string(3) "EQK"
Output for 7.1.15
string(3) "TAZ" string(3) "UIF"
Output for 7.1.14
string(3) "1FF" string(3) "5AB"
Output for 7.1.13
string(3) "BMO" string(3) "5L7"
Output for 7.1.12
string(3) "8U8" string(3) "QTN"
Output for 7.1.11
string(3) "FRU" string(3) "WRG"
Output for 7.1.10
string(3) "UZL" string(3) "P29"
Output for 7.1.9
string(3) "Y2J" string(3) "1LX"
Output for 7.1.8
string(3) "44F" string(3) "I39"
Output for 7.1.7
string(3) "CUU" string(3) "4GM"
Output for 7.1.6
string(3) "ORL" string(3) "273"
Output for 7.1.5
string(3) "684" string(3) "FJB"
Output for 7.1.4
string(3) "VP9" string(3) "5LS"
Output for 7.1.3
string(3) "I1G" string(3) "74E"
Output for 7.1.2
string(3) "EN4" string(3) "BUQ"
Output for 7.1.1
string(3) "VRZ" string(3) "20M"
Output for 7.1.0
string(3) "M36" string(3) "G05"
Output for 7.0.28
string(3) "S0E" string(3) "P7M"
Output for 5.6.38
Fatal error: Call to undefined function random_int() in /in/JIEtL on line 20
Process exited with code 255.