How to generate unique order numbers in php

coldplay.xixi
Release: 2023-03-05 18:44:02
Original
3194 people have browsed it

How to generate unique order numbers in php: First, you can generate the order number in advance; then delete and save it; when you finally use it, just take one out and use it. The code is [$orderNo = date('YmdHis' ).substr(microtime(), 2, 5)].

How to generate unique order numbers in php

##Related learningRecommendation:php programming(Video)

How to generate unique order numbers in php:

Idea: In order to prevent duplication, you can pre-generate order numbers for deduplication. Save it. For example, when saving the redis queue, just take one out and use it.

1. Preliminary - this may be repeated if there is a coincidence

function genRequestSn($unique=0){
        $orderNo = date('YmdHis').substr(microtime(), 2, 5) . mt_rand(10000,99999);
        return $orderNo;
    }
Copy after login

2. Process it and use a unique identifier such as The user id is spliced ​​after the order number so that the order number will basically not be repeated according to the user's steps, but it may still be repeated, which is basically OK

function genRequestSn($unique=0){
        $orderNo = date('YmdHis').substr(microtime(), 2, 5) . mt_rand(10000,99999);
        if(!empty($unique)) $orderNo = $orderNo.$unique;
        return $orderNo;
    }
Copy after login

If you want to learn more about programming, please stay tuned

phptraining column!

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

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template