Home Backend Development PHP Tutorial Summary of methods for generating unique non-repeating numbers in PHP

Summary of methods for generating unique non-repeating numbers in PHP

Dec 26, 2017 pm 01:56 PM
php method

How does PHP generate a unique and non-repeating number? 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);
Copy after login

Second type

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

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,&#39;0&#39;,STR_PAD_LEFT);
Copy after login

Fourth type:

After searching on the Internet, I 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, month, day, Unix timestamp and microseconds, random numbers, the possibility of repetition 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.

<?php 
$yCode = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;);
$orderSn = $yCode[intval(date(&#39;Y&#39;)) - 2011] . strtoupper(dechex(date(&#39;m&#39;))) . date(&#39;d&#39;) . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(&#39;%02d&#39;, rand(0, 99));
?>
Copy after login

Generation effect:

A422694333616096
Copy after login

Alas, it’s a pity that this method was not used in the final project, saying that it was unnecessary. complex, - -!

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

Related recommendations:

PHP random number C extended random number

Based on in-depth understanding of php random numbers_PHP tutorial

What is the correct posture for PHP to generate UUID?

The above is the detailed content of Summary of methods for generating unique non-repeating numbers in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles