Hướng dẫn php get unique number

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.

Nội dung chính

  • How do you generate a unique number?
  • How do I generate a random 10 digit number in PHP?
  • How do I generate a random 4 digit number in PHP?
  • How do I generate a random 6 digit number in PHP?

----------
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

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

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 . "
"; }

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]


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.


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:

Chủ Đề