SoFunction
Updated on 2025-03-10

PHP generates random strings (3 methods)

If a user registers to generate a random password, the user needs to generate a random password to reset the password. Random passwords are also a string of fixed-length strings. The article has compiled several methods to generate random strings.
Method 1

1. Generate a random integer in 33-126, such as 35.

2. Convert 35 into the corresponding ASCII character, such as 35 corresponding #.

3. Repeat the above 1 and 2 steps n times to connect to n-bit password.

This algorithm mainly uses two functions. The mt_rand ( int $min , int $max ) function is used to generate random integers, where $min – $max is the range of ASCII code. Here, 33 -126 is taken, and the range can be adjusted as needed. For example, the English letters corresponding to a – z in the ASCII code table are 97 – 122 bits. For details, please refer to the ASCII code table; the chr ( int $ascii ) function is used to convert the corresponding integer $ascii into the corresponding characters.

function create_password($pw_length =  
{ 
$randpwd = ”; 
for ($i = 0; $i < $pw_length; $i++) 
{ 
$randpwd .= chr(mt_rand(33, 126)); 
} 
return $randpwd; 
} 
// Call this function and pass the length parameter $pw_length = 6echo create_password(6); 

Method 2

1. Preset a string $chars, including a – z, A – Z, 0 – 9, and some special characters.

2. Take a random character in the $chars string.

3. Repeat the second step n times to get a password of length n.

function generate_password( $length = 8 ) { 
// Password character set, you can add any characters you need$chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|'; 
$password = ”; 
for ( $i = 0; $i < $length; $i++ ) 
{ 
// Here are two ways to get characters// The first one is to use substr to intercept any one of the characters in $chars;// The second type is to take any element of the character array $chars// $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); 
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; 
} 
return $password; 
} 

Method 3

1. Preset a character array $chars, including a – z, A – Z, 0 – 9, and some special characters.

2. Randomly select $length elements from the array $chars through array_rand().

3. According to the obtained key name array $keys, extract the character splicing string from the array $chars. The disadvantage of this method is that the same characters will not be taken repeatedly.

function make_password( $length = 8 ) 
{ 
 
// Password character set, you can add any characters you need$chars = array(‘a', ‘b', ‘c', ‘d', ‘e', ‘f', ‘g', ‘h', 
‘i', ‘j', ‘k', ‘l','m', ‘n', ‘o', ‘p', ‘q', ‘r', 's', 
‘t', ‘u', ‘v', ‘w', ‘x', ‘y','z', ‘A', ‘B', ‘C', ‘D', 
‘E', ‘F', ‘G', ‘H', ‘I', ‘J', ‘K', ‘L','M', ‘N', ‘O', 
‘P', ‘Q', ‘R', ‘S', ‘T', ‘U', ‘V', ‘W', ‘X', ‘Y','Z', 
‘0′, ‘1′, ‘2′, ‘3′, ‘4′, ‘5′, ‘6′, ‘7′, ‘8′, ‘9′, ‘!', 
‘@','#', ‘$', ‘%', ‘^', ‘&', ‘*', ‘(‘, ‘)', ‘-', ‘_', 
‘[', ']‘, ‘{‘, ‘}', ‘<', ‘>', ‘~', ‘`', ‘+', ‘=', ‘,', 
‘.', ‘;', ‘:', ‘/', ‘?', ‘|'); 
 
// Randomly take $length array element key names in $chars$keys = ($chars, $length); 
$password = ”; 
for($i = 0; $i < $length; $i++) 
{ 
// Concatenate $length array elements into strings$password .= $chars[$keys[$i]]; 
} 
return $password; 
} 

Time efficiency comparison

We use the following PHP code to calculate the run time of the above 3 random password generation functions to generate 6-bit passwords, and then make a simple comparison of their time efficiency. The final result is:

Method 1: 9.8943710327148E-5 seconds
Method 2: 9.6797943115234E-5 seconds
Method 3: 0.00017499923706055 seconds
It can be seen that the execution time of Method 1 and Method 2 is similar, while the running time of Method 3 is slightly longer.
By comparing the implementation process of the three methods and comparing time efficiency, we learned three methods of PHP to generate random strings. I hope it will be helpful to everyone's future learning.