php获取四位字母和数字的随机数的实现方法_PHP教程

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

php获取四位字母和数字的随机数的实现方法

 这篇文章主要介绍了php做程序开发的过程中,我们很多时候会在登录界面或者评论界面做一些四位数的验证码,需要的朋友可以参考下

 

 

那么我们知道在php中简单的四位数的纯数字验证可以用rand(1000,9999)就可以了,但如果我们要得到字母和数字的随机四位数,那我们该如何写函数呢?下面胡鹏博客在php资料栏目下给出一个完整的实例。

?

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

{

$outputstr .= $chars_array[mt_rand(0, $charsLen)];

}

return $outputstr;

}

echo GetfourStr(4);

?>

其中部分函数解析:mt_rand函数说明:mt_rand()返回随机整数。
如果没有提供可选参数 min 和 max,mt_rand() 返回 0 到 RAND_MAX 之间的伪随机数。例如想要 0 到 46(包括 0 和 46)之间的随机数,用 mt_rand(0, 46)。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/971919.htmlTechArticlephp获取四位字母和数字的随机数的实现方法 这篇文章主要介绍了php做程序开发的过程中,我们很多时候会在登录界面或者评论界面做一些四...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!