PHP获取6位数随机数,获取redis里面不存在的6位随机数(设置24小时过时)

PHP获取6位数随机数

PHP str_shuffle() 函数
str_shuffle() 函数随机打乱字符串中的所有字符。 语法 str_shuffle(string) 参数 描述 string必需。规定要打乱的字符串。

用php的str_shuffle函数:
<?php
$randStr = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
$rand = substr($randStr,0,6);
?>
------------------------------
实例:获取redis里面不存在的6位随机数(设置24小时过时)
$port_number = '1605D1BCC6C8027BA0223147652D67D6';
$send_number = $this->getSixRandNumber();
$rs = $this->redis->setKeyValue('ports:' . $send_number,$port_number);
//以秒为最小单位
$this->redis->setTimeout('ports:' . $send_number,24*3600);

/**
* 获取6位数随机数
*/
protected function getSixRandNumber(){
$randStr = str_shuffle('1234567890');
$rand = substr($randStr,0,6);
$port = $this->redis->getItemByKey('ports:' .$rand);
//存在的重新取
if($port != null){
return $this->getSixRandNumber();
}
return $rand;
}

原文地址:https://www.cnblogs.com/zdz8207/p/php-str_shuffle-rand-redis.html