PHP to get four-digit letters and numbers of random number implementation method _php tips

Source: Internet
Author: User

So we know that simple four-digit pure digital verification in PHP can be done with Rand (1000,9999), but if we're going to get a random four-digit number of letters and numbers, how do we write a function? The following Hu Peng blog in the PHP data column gives a complete example.

<?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);
? >

Part of the function parsing: mt_rand function Description: Mt_rand () returns a random integer.
If the optional parameter min and Max,mt_rand () are not provided, the pseudo random number between 0 and Rand_max is returned. For example, want a random number between 0 and 46 (including 0 and 46), with Mt_rand (0, 46).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.