How to get a random number with four letters and numbers in PHP (verification code)

怪我咯
Release: 2023-03-13 14:20:02
Original
2501 people have browsed it

This article mainly introduces the process of PHP program development. We often make some four-digit verification codes on the login interface or comment interface. Friends who need it can refer to it

So we know that simple four-digit pure number verification in php can use rand(1000,9999), but if we want to get a random four-digit number of letters and numbers, then how should we write What about function? Hu Peng's blog below gives a complete example under the PHP information column.

<?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);
?>
Copy after login

Part of the function analysis: mt_rand function description: mt_rand() returns a random integer.
If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random number between 0 and RAND_MAX. For example, if you want a random number between 0 and 46 (inclusive), use mt_rand(0, 46).

The above is the detailed content of How to get a random number with four letters and numbers in PHP (verification code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!