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

little bottle
Release: 2023-04-06 06:04:02
forward
2754 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template