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(5)); var_dump(random_str(5, 'abcdefghijklmnopqrstuvwxyz'));
based on KKlc3
Output for 7.2.6
string(5) "0MNbj" string(5) "rhdpr"
Output for 7.2.0
string(5) "vRQdD" string(5) "yyyvr"
Output for 7.1.10
string(5) "lxBMg" string(5) "tydxz"
Output for 7.1.7
string(5) "NI8ze" string(5) "nretu"
Output for 7.1.6
string(5) "cGhNN" string(5) "wnmtk"
Output for 7.1.5
string(5) "Uv6zU" string(5) "aguta"
Output for 7.1.4
string(5) "L5YWi" string(5) "bocyi"
Output for 7.1.3
string(5) "5sDza" string(5) "prxqj"
Output for 7.1.2
string(5) "ZLYMQ" string(5) "bqzry"
Output for 7.1.1
string(5) "qqZ7n" string(5) "hzyrs"
Output for 7.1.0
string(5) "NYXGB" string(5) "lpzzy"
Output for 7.0.20
string(5) "vsDPe" string(5) "ovjwe"
Output for 7.0.19
string(5) "hfrRR" string(5) "kgiyw"
Output for 7.0.18
string(5) "1lsUh" string(5) "sewrv"
Output for 7.0.17
string(5) "4OH0R" string(5) "rgpsb"
Output for 7.0.16
string(5) "CqHFe" string(5) "tipcd"
Output for 7.0.15
string(5) "BDYh0" string(5) "hejkz"
Output for 7.0.14
string(5) "AWLcg" string(5) "dkeep"
Output for 7.0.13
string(5) "SmTLV" string(5) "etwnq"
Output for 7.0.12
string(5) "HnenY" string(5) "aglya"
Output for 7.0.11
string(5) "Ec3vY" string(5) "xurgw"
Output for 7.0.10
string(5) "1F6xS" string(5) "nuwcx"
Output for 7.0.9
string(5) "eohGP" string(5) "wwaec"
Output for 7.0.8
string(5) "8BnKi" string(5) "mgnfl"
Output for 7.0.7
string(5) "xlUp8" string(5) "dzavy"
Output for 7.0.6
string(5) "JNFgH" string(5) "oaawr"
Output for 7.0.5
string(5) "D48SA" string(5) "wsejk"
Output for 7.0.4
string(5) "9byuO" string(5) "pjprz"
Output for 7.0.3
string(5) "NmVA4" string(5) "vwefr"
Output for 7.0.2
string(5) "qYaZR" string(5) "wolch"
Output for 7.0.1
string(5) "MbNFs" string(5) "ybwvk"
Output for 7.0.0
string(5) "QJ4Yl" string(5) "sqaxv"
Output for hhvm-3.15.4
string(5) "cSnGI" string(5) "pjptz"
Output for 5.6.0 - 5.6.30
Fatal error: Call to undefined function random_int() in /in/cdGXe on line 20
Process exited with code 255.