Home  >  Article  >  Backend Development  >  Introduction to how to generate random strings in php

Introduction to how to generate random strings in php

零下一度
零下一度Original
2017-07-18 16:44:191458browse

Using PHP to develop applications, especially website programs, often requires generating random passwords, and this article collects and sorts out methods for generating random strings

 generateRandomString( = 10 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' = '' ( = 0;  < ; ++ .= [(0, () - 1 generateRandomString(20);
/**
 * 随机生成字符串
 * @param int $length
 * @return null|string
 */private static function getRandChar($length = 8){
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol)-1; for($i=0;$i<$length;$i++){
        $str.=$strPol[rand(0,$max)]; //rand($min,$max)生成介于min和max两个数之间的一个随机整数
    } return $str;
}

The above is the detailed content of Introduction to how to generate random strings in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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