Function for generating random numbers using PHP
- Function randomkeys ($ length)
- {
- $ Pattern = '1234567890abcdefghijklmnopqrstuvwxyz
- ABCDEFGHIJKLOMNOPQRSTUVWXYZ,./& l
- T;> ?; #:@~ [] {}-_ = +) (* & ^ % ___ FCKpd ___ 0 pound ;"! '; // Character pool
- For ($ I = 0; $ I <$ length; $ I ++)
- {
- $ Key. = $ pattern {mt_rand ()}; // Generate a php random number
- }
- Return $ key;
- }
- Echo randomkeys (8); This php random function can generate XC * = z ~ A string like 7L is random enough! Now we will introduce another method to generate random numbers using PHP: Use the chr () function to save the steps to create a character pool. Function randomkeys ($ length)
- {
- $ Output = ";
- For ($ a = 0; $ a <$ length; $ a ++ ){
- $ Output. = chr (mt_rand (33,126); // Generate a php random number
- }
- Return $ output;
- }
- Echo randomkeys (8 );
- ?>
Note: In the second php random function, use mt_rand () to generate a php random number between 33 and 126, and then convert it to a character using the chr () function. View the ascii code table and you will find that 33 to 126 represents all the characters in the character pool of the first function. The second function has the same functionality as the first function, and is more concise. |