Home >php教程 >php手册 >Thinkphp generates order number small case

Thinkphp generates order number small case

WBOY
WBOYOriginal
2016-09-30 09:22:551183browse

A small case of Thinkphp generating order numbers
Everyone will encounter the problem of order number generation in daily mall project development. Today, Thinkphp will lead you to interpret the problem of generating order numbers!
First of all, we need to make it clear that the order number has three properties: 1. Uniqueness 2. Unpredictability 3. Efficiency. Needless to say, uniqueness and unpredictability. Efficiency means that you cannot frequently query the database to avoid repeat.
Moreover, while meeting these conditions, the order number must be short enough. I don’t know if my friends have thought about some minor issues of order generation like me in their daily projects. You may also say that these things don’t need to be as complicated as you think. In fact, the editor agrees with everyone. But everyone knows that we all pay attention to rigor when making programs, and in the development of the order module, I believe everyone knows the location of the order number. Therefore, when we write these small programs, we might as well spend a few minutes to think about why this is the case. Go define! Okay, now I’ll tell you how to generate an order!
First of all, how do we generate an order? You can use a timestamp plus a random number, such as: time().rand(10000,99999); In this way, a 15-digit random number is generated, and the timestamp is accurate to milliseconds. The next five random numbers also eliminate the duplication of order numbers under high concurrency conditions. Of course, we can also simply process the timestamp into: date("YmdHis").rand(10000,99999); In this way, I believe friends have also noticed that we have been using a rand PHP random number function. So, when we learn the basics of PHP, when we encounter a random number function, do we still I'm thinking, what is the purpose of this function? Friends should understand now! Of course, we can also encapsulate it into a method for use in similar projects, which also improves the reusability of our daily code and improves the efficiency of our code a lot. So how to encapsulate it? The editor writes a simple example for everyone:
function get_sn() {
return date('YmdHis').rand(100000, 999999);
}
Of course, this is just a simple example. More and more fun small programs still need to be developed together by your friends!

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