Home  >  Article  >  Backend Development  >  Use PHP to randomly generate a string with a specified number of digits

Use PHP to randomly generate a string with a specified number of digits

little bottle
little bottleforward
2019-04-19 09:51:532718browse

The main content of this article is to use PHP language to randomly generate the required number of strings, including uppercase and lowercase letters and numbers. It has certain reference value. Interested friends can learn about it.

php randomly generates the required number of characters

/**
 * 随机生成要求位数个字符串
 * @param length 规定几位字符
 */
function getRandChar($length){
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";//大小写字母以及数字
    $max = strlen($strPol)-1;

    for($i=0;$i<$length;$i++){
        $str.=$strPol[rand(0,$max)];
    }
    return $str;
}

Related tutorials: PHP video tutorial

The above is the detailed content of Use PHP to randomly generate a string with a specified number of digits. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete