Home >Backend Development >PHP Problem >How to generate unique order numbers in php
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)].
##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 coincidencefunction 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 tunedphptraining 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!