Home>Article>Backend Development> How to generate unique serial numbers in php

How to generate unique serial numbers in php

藏色散人
藏色散人 Original
2021-03-03 09:28:37 3034browse

How to generate unique serial numbers in php: 1. Generate unique serial numbers through "str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);"; 2. Functions such as implode and array_map generate unique serial numbers.

How to generate unique serial numbers in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer.

Summary of methods for PHP to generate unique and non-repeating numbers

How does PHP generate unique and non-repeating numbers? Many times, for example, orders require us to generate a unique number. Under normal circumstances, the uniqid() provided by PHP can meet the needs, but in times of high concurrency, we need a better solution to generate unique and non-repeating numbers. This article will share example code, I hope it will be helpful to everyone.

There was an e-commerce project a while ago that required generating an order number. The consideration at the time was simple, take the system time and add a random number, or use the uniqid() method. Carefully consider the above method. When the customer purchases a small amount, the possibility of order duplication is zero. However, duplication of order numbers generated during the peak purchase period is very likely to occur.

First type

return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);

Second type

return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

Third type

//生成24位唯一订单号码,格式:YYYY-MMDD-HHII-SS-NNNN,NNNN-CC,其中:YYYY=年份,MM=月份,DD=日期,HH=24格式小时,II=分,SS=秒,NNNNNNNN=随机数,CC=检查码 @date_default_timezone_set("PRC"); while(true){ //订购日期 $order_date = date('Y-m-d'); //订单号码主体(YYYYMMDDHHIISSNNNNNNNN) $order_id_main = date('YmdHis') . rand(10000000,99999999); //订单号码主体长度 $order_id_len = strlen($order_id_main); $order_id_sum = 0; for($i=0; $i<$order_id_len; $i++){ $order_id_sum += (int)(substr($order_id_main,$i,1)); } //唯一订单号码(YYYYMMDDHHIISSNNNNNNNNCC) $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);

Fourth type Kind:

I searched online and found that this classmate’s idea is quite good, redtamo. Please go and have a look at the details. I will give a brief overview. This method uses English letters, year and month. Day, Unix timestamp and microseconds, random numbers, the possibility of duplication is greatly reduced, which is still very good. The use of letters is very representative. One letter corresponds to a year, a total of 16 digits, no more, no less, haha.

Generation effect:

A422694333616096

Alas, it is a pity that this method was not used in the final project, saying that there was no need to be so complicated, - -!

The above four methods are all the content shared with you in this article. I hope you will like it.

【Recommended:PHP Video Tutorial

The above is the detailed content of How to generate unique serial numbers in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn