简体   繁体   中英

How to generate an unique 8-digit integer number?

I want to generate a 8 digit random integer number using the PHP mt_rand() function.

I know the PHP mt_rand() function only takes 2 parameters: a minimum and a maximum value.

How do I do this?

Try this ,

$num = str_pad(mt_rand(1,99999999),8,'0',STR_PAD_LEFT);

use str_pad with mt_rand

If you want exactly 8 digit numbers then you have to specify min and max value in mt_rand . Try it like below:

echo mt_rand(10000000,99999999);

So that it will always return 8 digit number. (between 10000000 & 99999999 )

You can do like this. It will return the number between the range 10000000 - 99999999

<?php
echo(mt_rand(10000000,99999999) . "<br>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM