Home  >  Article  >  php教程  >  php随机生成数字字母组合的方法,php生成数字字母

php随机生成数字字母组合的方法,php生成数字字母

WBOY
WBOYOriginal
2016-06-13 09:11:251179browse

php随机生成数字字母组合的方法,php生成数字字母

本文实例讲述了php随机生成数字字母组合的方法。分享给大家供大家参考。具体如下:

直接上代码:
复制代码 代码如下: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         $str .= $chars[mt_rand(0, $lc)]; 
    }
    return $str;
}

例如随机生成 2 位 字母和数字组合
只需调用函数 并传参2即可。

echo getRandomString(2);

如果仅仅是生成小写字母你可以使用类似方法

echo chr(mt_rand(65, 90);

大写字母

echo chr(mt_rand(97, 122));

希望本文所述对大家的php程序设计有所帮助。

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