Home >Backend Development >PHP Problem >How to generate unique order numbers in php

How to generate unique order numbers in php

coldplay.xixi
coldplay.xixiOriginal
2020-09-04 10:37:173239browse

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

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

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!

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