Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a 6 digit unique number?

Tags:

php

random

How can I generate a 6 digit unique number? I have verification mechanisms in place to check for duplicate entries.

like image 782
AAA Avatar asked Mar 28 '11 20:03

AAA


People also ask

How many unique numbers are there in 6 digits?

But with a six digit code, there are 1 million possible combos, making it a lot tougher for someone to crack your security code.


2 Answers

$six_digit_random_number = random_int(100000, 999999); 

As all numbers between 100,000 and 999,999 are six digits, of course.

like image 61
Charles Avatar answered Oct 08 '22 05:10

Charles


If you want it to start at 000001 and go to 999999:

$num_str = sprintf("%06d", mt_rand(1, 999999)); 

Mind you, it's stored as a string.

like image 29
Tim Cooper Avatar answered Oct 08 '22 07:10

Tim Cooper