Home >Backend Development >PHP Tutorial >PHP method to obtain a specified number of random strings
This article mainly introduces the method of obtaining a specified number of random strings in PHP, involving PHP's operation skills related to array traversal and string operations. Friends in need can refer to it
The details are as follows:
function getARandLetter($number = 1) { if ($number == 0) return FALSE; //去除0 $number = $number < 0 ? - $number : $number; //如果小于零取正值 $letterArr = 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', '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' ); $returnStr =''; for($i= 0; $i < $number; $i ++) { $returnStr .= $letterArr [rand ( 0, 51 )]; } return $returnStr; } echo getARandLetter(8);
Running results: lUJfScvS
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP implementationRandomlyGet domain names that are not blocked by WeChat
PHP method to implement randomgenerate watermark image function
php Get random## from the specified number #Combined method
The above is the detailed content of PHP method to obtain a specified number of random strings. For more information, please follow other related articles on the PHP Chinese website!