Home >php教程 >php手册 >php生成随机数或者字符串的代码

php生成随机数或者字符串的代码

WBOY
WBOYOriginal
2016-06-13 12:26:581047browse

$len表示长度,代码如下:

复制代码 代码如下:


/**
* 产生随机字符串
*
* 产生一个指定长度的随机字符串,并返回给用户
*
* @access public
* @param int $len 产生字符串的位数
* @return string
*/
function randstr($len=6) {
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789-@#~';
// characters to build the password from
mt_srand((double)microtime()*1000000*getmypid());
// seed the random number generater (must be done)
$password='';
while(strlen($password)$password.=substr($chars,(mt_rand()%strlen($chars)),1);
return $password;
}

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