Home >Backend Development >PHP Tutorial >How to get random numbers with four letters and numbers in PHP_PHP Tutorial
This article mainly introduces the process of PHP program development. We often use the login interface or Make some four-digit verification codes in the comment interface. Friends who need it can refer to it
So we know that simple four-digit pure number verification in php can be done with rand(1000,9999), but if we want to get random four-digit numbers of letters and numbers, then how should we write a function Woolen cloth? Hu Peng's blog below gives a complete example under the PHP information column.
?
3 6 13 14 |
<๐>function GetfourStr($len)<๐> <๐>{<๐> <๐>$chars_array = array(<๐> <๐>"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",<๐> <๐>"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",<๐> <๐>);<๐> <๐>$charsLen = count($chars_array) - 1;<๐> <๐> <๐> <๐>$outputstr = "";<๐> <๐>for ($i=0; $i<$len; $i )<๐> <๐>{<๐> <๐>$outputstr .= $chars_array[mt_rand(0, $charsLen)];<๐> <๐>}<๐> <๐>return $outputstr;<๐> <๐>}<๐> <๐>echo GetfourStr(4);<๐> <๐>?> |