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

WBOY
Release: 2016-07-13 10:01:43
Original
873 people have browsed it

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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template