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 = '975013246') { $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(6));
based on XCkjZ
Output for 7.2.6
string(6) "117690"
Output for 7.2.0
string(6) "665272"
Output for 7.1.10
string(6) "206514"
Output for 7.1.7
string(6) "997519"
Output for 7.1.6
string(6) "160579"
Output for 7.1.5
string(6) "096392"
Output for 7.1.4
string(6) "034019"
Output for 7.1.3
string(6) "241293"
Output for 7.1.2
string(6) "660621"
Output for 7.1.1
string(6) "067376"
Output for 7.1.0
string(6) "594791"
Output for 7.0.20
string(6) "636605"
Output for 7.0.19
string(6) "951479"
Output for 7.0.18
string(6) "271904"
Output for 7.0.17
string(6) "739209"
Output for 7.0.16
string(6) "621327"
Output for 7.0.15
string(6) "172613"
Output for 7.0.14
string(6) "413004"
Output for 7.0.13
string(6) "654431"
Output for 7.0.12
string(6) "357243"
Output for 7.0.11
string(6) "212521"
Output for 7.0.10
string(6) "550575"
Output for 7.0.9
string(6) "907047"
Output for 7.0.8
string(6) "056323"
Output for 7.0.7
string(6) "404416"
Output for 7.0.6
string(6) "523546"
Output for 7.0.5
string(6) "640301"
Output for 7.0.4
string(6) "065154"
Output for 7.0.3
string(6) "139213"
Output for 7.0.2
string(6) "724424"
Output for 7.0.1
string(6) "405062"
Output for 7.0.0
string(6) "147219"
Output for hhvm-3.15.4
string(6) "062001"
Output for 5.6.0 - 5.6.30
Fatal error: Call to undefined function random_int() in /in/Nj2SV on line 23
Process exited with code 255.