Home >Backend Development >PHP Tutorial >Detailed explanation of how to obtain a specified number of random strings in php
This article mainly introduces the method of PHP to obtain a specified number of random strings, involving PHP's array traversal and string operation related operation skills. Friends in need can refer to it. Next
The example in this article describes the method of obtaining a specified number of random strings in PHP. Share it with everyone for your reference, 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);
The above is the detailed content of Detailed explanation of how to obtain a specified number of random strings in php. For more information, please follow other related articles on the PHP Chinese website!