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 = shuffle_str('0123456789')) { $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(32)); var_dump(random_str(8, 'abcdefghijklmnopqrstuvwxyz'));
Output for 7.0.0 - 7.1.7
Fatal error: Constant expression contains invalid operations in /in/4jWkE on line 15
Process exited with code 255.
Output for hhvm-3.15.4
Fatal error: Uncaught Error: Call to undefined function shuffle_str() in /in/4jWkE:15 Stack trace: #0 /in/4jWkE(25): random_str() #1 {main}
Process exited with code 255.
Output for 5.6.0 - 5.6.30
Parse error: syntax error, unexpected '(', expecting ')' in /in/4jWkE on line 15
Process exited with code 255.