How to generate a unique number in php?

I am generating random numbers using php random function, but I want the generated number should be unique and it should not be repeated again.

----------
php code
 $number = rand(100,100000); //a six digit random number between 100 to 100000 
echo $number;

----------

but I am using this function for multiple times in my code for users so at very rare case there should be a chance of generating same number again. how can i avoid that.

asked Apr 10, 2018 at 3:36

How to generate a unique number in php?

nithin singh Gmnithin singh Gm

1291 gold badge2 silver badges10 bronze badges

8

I would do this:

You said you have branches. The receipt id could look something like this:

$dateString = date('Ymd'); //Generate a datestring.
$branchNumber = 101; //Get the branch number somehow.
$receiptNumber = 1;  //You will query the last receipt in your database 
//and get the last $receiptNumber for that branch and add 1 to it.;

if($receiptNumber < 9999) {

  $receiptNumber = $receiptNumber + 1;

}else{
 $receiptNumber = 1;
} 

Update the receipt database with the receipt number.

$dateString . '-' . $branchNumber . '-' . $receiptNumber;

This will read:

20180406-101-1 

This will be unique(Provided you do less than 10,000 transactions a day.) and will show your employees easily readable information.

answered Apr 10, 2018 at 4:40

1

If you are storing users in DB you should create column [ID] as primary key with auto increment and that would be best solution.

In other case I'd recommend you to simply store all user id's in ascending order from N to M by reading last ID and adding 1 to it because I see no real gain from random order that only adds complexity to your code.

answered Apr 10, 2018 at 5:21

How to generate a unique number in php?

NoOorZ24NoOorZ24

2,6241 gold badge12 silver badges31 bronze badges

There are many ways, example:

$freq = [];
$number = rand(100,100000);
$times = 10;
while($times-- > 0)
{
   while(in_array($number, $freq))$number = rand(100,100000);
   $freq[] = $number;
   echo $number . "<br>";
}

This will print 10 random unique numbers.

answered Apr 10, 2018 at 3:41

Khaled AlamKhaled Alam

8427 silver badges12 bronze badges

random_int (PHP 7)

<?php
$number =  random_int(100, 100000);
echo $number;

answered Apr 10, 2018 at 4:42

How to generate a unique number in php?

mwwebmwweb

6,9594 gold badges17 silver badges23 bronze badges

All you need to do is use timestamp in php as timestamp never cross each other hence it will always generate unique number.You can use time() function in php.

The time() function is used to format the timestamp into a human desired format. The timestamp is the number of seconds between the current time and 1st January, 1970 00:00:00 GMT. It is also known as the UNIX timestamp.

<?php
$t=time();
echo $t;
?>

Also you add a rand() function and insert it in front of the $t to make it more random as if few users work at same time then the timestamp might collide.

<?php
$number = rand(100,100000);
$t=time();
$random = $number.''.$t;

echo $random;
?>

The above will reduce the chance to timestamp collide hence making the probability of number uniqueness almost 100%.

And if you make your column unique in your database then the php wont insert the number hence this bottleneck will ensure you will always get a unique random number.

 bill_id not null unique

answered Apr 10, 2018 at 3:48

Adern NerkAdern Nerk

3123 silver badges13 bronze badges

12

If you are using it for something like user id, then you can use uniqid for that. This command gets a prefixed unique identifier based on the current time in microseconds.

Here's how to use it:

string uniqid ([ string $prefix = "" [, bool $more_entropy = FALSE]] )

Where prefix is used if you are generating ids for a lot if hosts at the same time, you can use this to differentiate between various hosts if id is generated at the same microsecond.

more_entropy increases the likeness of getting unique values.

Usage:

<?php
/* A uniqid, like: 4b3403665fea6 */
printf("uniqid(): %s\r\n", uniqid());

/* We can also prefix the uniqid, this the same as 
 * doing:
 *
 * $uniqid = $prefix . uniqid();
 * $uniqid = uniqid($prefix);
 */
printf("uniqid('php_'): %s\r\n", uniqid('php_'));

/* We can also activate the more_entropy parameter, which is 
 * required on some systems, like Cygwin. This makes uniqid()
 * produce a value like: 4b340550242239.64159797
 */
printf("uniqid('', true): %s\r\n", uniqid('', true));
?>

answered Apr 10, 2018 at 4:23

this code must work

some description about code:

  1. generate unique id
  2. extract numbers form unique id with regex
  3. gathering numbers from regex with a loop
<?php

$unique = uniqid("",true);
preg_match_all("!\d+!", $unique ,$matches);
print_r($matches);
$numbers = "";
foreach($matches[0] as $key => $num){
   $numbers .= $num; 
}

echo $numbers;

answered Jun 15, 2021 at 4:54

How to generate a unique number in php?

How do you generate a unique number?

Here is how you can use the RAND function to generate a set of unique random numbers in Excel:.
In a column, use =RAND() formula to generate a set of random numbers between 0 and 1..
Once you have generated the random numbers, convert it into values, so that it won't recalculate again and again to make your workbook slow..

How do I generate a random 10 digit number in PHP?

The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.

How do I generate a random 4 digit number in PHP?

rand() or mt_rand() functions can be used to Generate 4 Digit Random Number in PHP.

How do I generate a random 6 digit number in PHP?

rand() or mt_rand() functions can be used to Generate 6 Digit Random Number in PHP.

Tải thêm tài liệu liên quan đến bài viết How to generate a unique number in php?