Home  >  Article  >  Backend Development  >  Implementation method of generating string random code in php_PHP tutorial

Implementation method of generating string random code in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:54:51805browse

php tutorial to generate string random code implementation method


*/
$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$index = mt_rand(0, 10);
echo $index;

//Look at a more complicated one

$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$validcharnumber = strlen($validcharacters);
$index = mt_rand(0, $validcharnumber-1);
$randomcharacter = $validcharacters[$index];
echo $randomcharacter;

//Example 2

$length = 5;
$result = "";
for ($i = 0; $i < $length; $i++) {
$index = mt_rand(0, $validcharnumber-1);
$result .= $validcharacters[$index];
}

//

function getrandomstring($length = 6) {
$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$validcharnumber = strlen($validcharacters);
$result = "";
for ($i = 0; $i < $length; $i++) { $index = mt_rand(0, $validcharnumber - 1); $index
$result .= $validcharacters[$index];

return $result;
}
echo getrandomstring();

http://www.bkjia.com/PHPjc/631732.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631732.htmlTechArticlephp tutorial to generate string random code implementation method*/ $validcharacters = abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#@!?; $index = mt_rand(0, 10); echo $index; //Look at a complex...
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