Reputation: 67
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?
Upvotes: 1
Views: 14159
Reputation: 16436
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
)
Upvotes: 3
Reputation: 1027
You can do like this. It will return the number between the range 10000000 - 99999999
<?php
echo(mt_rand(10000000,99999999) . "<br>");
Upvotes: 0
Reputation: 672
Try this,
$num = str_pad(mt_rand(1,99999999),8,'0',STR_PAD_LEFT);
use str_pad with mt_rand
Upvotes: 10