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 */ $keyspace = '0123456789'; function random_str($length, $keyspace = str_shuffle($keyspace)) { $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'));
based on QRXUP
Output for 7.0.0 - 7.1.7
Fatal error: Constant expression contains invalid operations in /in/7q0MP on line 19
Process exited with code 255.
Output for hhvm-3.15.4
Notice: Undefined variable: keyspace in /in/7q0MP on line 19 Warning: str_shuffle() expects parameter 1 to be string, null given in /in/7q0MP on line 19 Warning: mb_strlen() expects parameter 1 to be string, null given in /in/7q0MP on line 22 Fatal error: Uncaught Error: Minimum value must be less than or equal to the maximum value in /in/7q0MP:24 Stack trace: #0 /in/7q0MP(28): 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/7q0MP on line 19
Process exited with code 255.