- Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.7
- Fatal error: Constant expression contains invalid operations in /in/4jWkE on line 15
Process exited with code 255.
<?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'));