php生成随机数

    //获得随机数
    public function getStr()
    {
        $result = '';
        $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
        for ($i = 0; $i < 16; $i++) {
            $result .= $str[rand(0, 48)];
        }
        return $result;
    }

第二种

    //生成随机字符串
    private function createNonceStr($length = 8)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return "z".$str;
    }

 

你可能感兴趣的:(PHP基础)