Piyush Kukadiya
Piyush Kukadiya

Reputation: 67

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?

Upvotes: 1

Views: 14159

Answers (3)

B. Desai
B. Desai

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

5eeker
5eeker

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

Harisudhan. A
Harisudhan. A

Reputation: 672

Try this,

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

use str_pad with mt_rand

Upvotes: 10

Related Questions