Home >Backend Development >PHP Tutorial >How to randomly generate a combination of numbers and letters in php, generate numbers and letters in php_PHP tutorial

How to randomly generate a combination of numbers and letters in php, generate numbers and letters in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:02:481380browse

How to randomly generate a combination of numbers and letters in php, how to generate a combination of numbers and letters in php

The example in this article describes how to randomly generate a combination of numbers and letters in php. Share it with everyone for your reference. The details are as follows:

Go directly to the code:
Copy code The code is as follows: function getRandomString($len, $chars=null)
{
If (is_null($chars)){
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
}  
mt_srand(10000000*(double)microtime());
for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i ){
          $str .= $chars[mt_rand(0, $lc)];
}
Return $str;
}

For example, randomly generate a 2-digit letter and number combination
Just call the function and pass parameter 2.

echo getRandomString(2);

If you only generate lowercase letters, you can use a similar method

echo chr(mt_rand(65, 90);

Capital letters

echo chr(mt_rand(97, 122));

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/969495.htmlTechArticleHow to randomly generate a combination of numbers and letters in php, how to generate a combination of numbers and letters in php. This article describes how php randomly generates a combination of numbers and letters. method. Share it with everyone for your reference. The details are as follows:...
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