# Method 1functionmake_password ($ length8) {$ rangearray_merge (range (a, z), range (A, Z), range (0, 9); shuffle ($ range ); $ rangearray_slice ($ range, 0, $ length); $ passwordimplode (, $ range); return $ password;} rating:
# Method 1 function make_password ($ length = '8') {$ range = array_merge (range ('A', 'z'), range ('A ', 'Z'), range ('0', '9'); shuffle ($ range); $ range = array_slice ($ range, 0, $ length ); $ password = implode ('', $ range); return $ password;} rating:
# Method 1
function make_password( $length = '8'){ $range = array_merge( range('a', 'z') , range('A', 'Z') , range('0','9')); shuffle($range); $range = array_slice($range,0, $length); $password = implode('' , $range); return $password;}Rating: It looks comfortable and can be customized as needed. The disadvantage is low efficiency.
# Method 2
Function generate_password ($ length = 8) {// you can add the character set $ chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 as needed! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | '; $ Password = ''; $ chars_max_index = strlen ($ chars)-1; for ($ I = 0; $ I <$ length; $ I ++) {// two character acquisition methods are provided here. // The first method is to use substr to intercept any character in $ chars; // The second type is to take any element of the character array $ chars // $ password. = substr ($ chars, mt_rand (0, $ chars_max_index), 1); $ password. = $ chars [mt_rand (0, $ chars_max_index)];} return $ password ;}Rating: efficient, because only one
mt_rand()Function. You can customize the character set as needed, but it looks messy.
# Method 3
function create_password($pw_length = 8){ $randpwd = ''; for ($i = 0; $i < $pw_length; $i++) { $randpwd .= chr(mt_rand(33, 126)); } return $randpwd;}Rating: very efficient, with ASCII code and
mt_rand(),
chr()Complete the random string. You can customize the character set as needed.
# Method 4
function get_password( $length = 8 ){ $str = substr(md5(time()), 0, 6); return $str;}Comment: Nima.
Original article address: several methods for generating random strings in PHP. Thank you for sharing it with the original author.