【PHP原生】生成随机字符串或数字

1、生成3位随机字符串

<?php
$str = randStr(3);
echo $str;
function randStr($i)
{
    $str = "abcdefghijklmnopqrstuvwxyz0123456789";
    $finalStr = "";
    for ($j = 0; $j < $i; $j++) {
        $finalStr .= substr($str, rand(0, 37), 1);
    }
    return $finalStr;
}

2、生成随机数

<?php
function generate_code($length = 6)
{
    return rand(pow(10, $length - 1), pow(10, $length) - 1);
}

 

 

 

posted @ 2018-10-16 09:57  PHPer_Cody  阅读(293)  评论(0编辑  收藏  举报