下面是随机字符串生成的代码,它正在工作但是这里有一些问题,我现在无法弄清楚这里发生的是它总是返回长度为1的值,我期待一个长度为10的随机字符串.我也传递10长度.请指导我在这里做错了什么.
<?php
function random_string($length) {
$len = $length;
$base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
$max = strlen($base) - 1;
$activatecode = '';
mt_srand((double) microtime() * 1000000);
while (strlen($activatecode) < $len + 1) {
$activatecode.=$base{mt_rand(0, $max)};
return $activatecode;
}
}
?>
Run Code Online (Sandbox Code Playgroud)