This article describes how to generate a random combination of numbers and letters in php. The example analyzes the skills and usage of generating random numbers and random letters in php, which is of great practical value, for more information about how to generate a random combination of numbers and letters in php, see the following example. Share it with you for your reference. The details are as follows:
Directly run the code:
The code is as follows:
Function getRandomString ($ len, $ chars = null)
{
If (is_null ($ chars )){
$ Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
}
Mt_srand (10000000 * (double) microtime ());
For ($ I = 0, $ str = '', $ lc = strlen ($ chars)-1; $ I <$ len; $ I ++ ){
$ Str. = $ chars [mt_rand (0, $ lc)];
}
Return $ str;
}
For example, a random combination of two letters and numbers is generated.
You only need to call the function and pass parameter 2.
Echo getRandomString (2 );
You can use a similar method to generate only lower-case letters.
Echo chr (mt_rand (65, 90 );
Uppercase letters
Echo chr (maid (97,122 ));
I hope this article will help you with php programming.