tp5 生成随机数

控制器调用

        public function GetRanStr(){
            if (request()->isPost()) {
          //生成6位数随机数
return GetRandStr(6);
       }
     }

 

 

公共方法

/**
 * 生成随机数
 * @param $len
 * @return string
 */
function GetRandStr($len) {
    $chars = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k","l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v","w", "x", "y", "z","0", "1", "2","3", "4", "5", "6", "7", "8", "9");
    $charsLen = count($chars) - 1;
    shuffle($chars);
    $output = "";
    for ($i=0; $i<$len; $i++){
        $output .= $chars[mt_rand(0, $charsLen)];
    }
    return $output;
}

 

posted @ 2019-08-23 17:34  钧一  阅读(3686)  评论(0编辑  收藏  举报