Hello readers, today we discuss about "Generate a random, unique password or alphanumeric string" using Php function.
function random_string($len) {
$key = '';
$keys = array_merge(range(0, 9));
for ($i = 0; $i < $len; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
echo random_string(4);
In above code the password should be only come in integer and the length is 4 digit.
If you want alphanumeric string and the output length more than 4 so you can use the below code.
function random_string($len) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));
for ($i = 0; $i < $len; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
echo random_string(10);
0 Comment(s)