PHP generate random alphanumeric string

To generate random alphanumeric string there are many ways to generate a random, unique, alphanumeric string in PHP. The following ways to generate random alphanumeric string

Using str_shuffle() Function: 

The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter. When a number is passed, it treats the number as the string and shuffles it. This function does not make any change in the original string or the number passed to it as a parameter. Instead, it returns a new string which is one of the possible permutations of the string passed to it in the parameter.


$length = 10;
$data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';

echo substr(str_shuffle($used_symbols), 0, $length);
echo "<br />";
echo substr(str_shuffle($used_symbols), 0, 8);


2 y fd5eIFrh

Using md5() Function:

The md5() function is used to calculate the MD5 hash of a string. Pass timestamp as a argument and md5 function will convert them into 32 bit characters

echo substr(md5(microtime()), 0, 10); 
echo substr(md5(microtime()), 0, 8); 

// output

Recommended Posts: