Home  >  Article  >  Backend Development  >  How to get a four-digit random number of letters and numbers in PHP, four-digit random number_PHP tutorial

How to get a four-digit random number of letters and numbers in PHP, four-digit random number_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:591038browse

How to obtain four-digit random number of letters and numbers in php, four-digit random number

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.

<&#63;php
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);
&#63;>

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).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/940489.htmlTechArticlephp implementation method of obtaining four-digit random numbers of letters and numbers, four-digit random numbers then we know in php For simple four-digit pure numerical verification, rand(1000,9999) can be used, but if...
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