Home >Backend Development >PHP Tutorial >How to get random numbers with four letters and numbers in PHP_PHP Tutorial

How to get random numbers with four letters and numbers in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:01:43878browse

How to obtain random numbers with four letters and numbers in PHP

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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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);

?>

1 2

3

4 5

6

8 9 10 11 12
13 14
15 16 17 18 19 20 21 22
<๐ŸŽœ>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);<๐ŸŽœ> <๐ŸŽœ>?>
Part of the function analysis: mt_rand function description: mt_rand() returns a random integer. If the optional min and max arguments are not provided, mt_rand() returns a pseudorandom number between 0 and RAND_MAX. For example, if you want a random number between 0 and 46 (inclusive), use mt_rand(0, 46). http://www.bkjia.com/PHPjc/971919.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/971919.htmlTechArticleHow to obtain random numbers with four letters and numbers in php. This article mainly introduces the method of program development in php In the process, we often do some four things on the login interface or comment interface...
Statement๏ผš
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn