<?php // 要生成随机码位数 $length = 4; $start = pow(10, $length - 1); $end = pow(10, $length) - 1; echo $start . '-' . $end . PHP_EOL; // 1000-9999 echo rand($start, $end); // 5921
<?php // 要生成随机码位数 $length = 4; $start = pow(10, $length - 1); $end = pow(10, $length) - 1; echo $start . '-' . $end . PHP_EOL; // 1000-9999 echo rand($start, $end); // 5921