This article mainly introduces the method of PHP to obtain 6-digit random numbers that do not exist in redis. It can set a 24-hour expiration limit and involves PHP string and database related operation skills. Friends who need it can refer to it
PHP Gets a 6-digit random number
PHP str_shuffle()
Function
str_shuffle() function randomly shuffles Garble all characters in the string.
Parameter Description | |
---|---|
Required. Specifies the string to be scrambled. |
<?php $randStr = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'); $rand = substr($randStr,0,6); ?>
Example: Get redis A 6-digit random number that does not exist in it (set to expire in 24 hours)
$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; }
Related recommendations:
php generatesrandom numberscharacters, letters or mixed strings of numbers and letters
Random numbers and detailed explanation of random sequence methods
##What PHP generates
The above is the detailed content of How to get a 6-digit random number that does not exist in redis with PHP. For more information, please follow other related articles on the PHP Chinese website!