随机字符串生成php

noo*_*php 0 php random

下面是随机字符串生成的代码,它正在工作但是这里有一些问题,我现在无法弄清楚这里发生的是它总是返回长度为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)

mat*_*lie 6

你从while内返回,导致while循环只运行一次并返回结果(只有1个字符)

将你的返回线1向下移动(从while循环中移出)它应该可以工作.