PHP 随机字符串 可刷新运行 刷新

随机指定长度的带有大小写和数字的字符串

代码:

var_export(getRandomString(10));

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;
    }

输出:

'zZAgeKBzsS'