This article mainly introduces the PHP random generation number of two methods, interested in the reference of friends, I hope to be helpful to everyone.
The first method uses Mt_rand ():
function Getrandstr ($length) {$str = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '; $len =strlen ( $STR)-1; $randstr = "; for ($i =0; $i < $length; $i + +) {$num =mt_rand (0, $len); $randstr. = $str [$num];} return $randstr; } $number =getrandstr (6); Echo $number;
The second method (fastest)
function make_password ($length = 8) {//password character set, you can add any character 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 ', '! ', ' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', '-', ' _ ' , ' [', '] ', ' {', '} ', ' < ', ' > ', ' ~ ', ' ', ' ' + ', ' = ', ', ', '. ', '; ', ': ', '/', '? ', ' | '; $length number of element key names are randomly taken in the $chars $keys = Array_rand ($chars, $length); $password = "; for ($i = 0; $i < $length; $i + +) {//To concatenate $length array elements into a string $password. = $chars [$keys [$i]]; } return $password; }