thinkphp Generate order number small case
Small Partners in the Daily Mall project development, will encounter the issue of order number generation, today, think of PHP will lead you to read the issue of generating order number!
first of all, the order number we want to make clear that it has 3 properties: 1. Uniqueness 2. Non-speculative 3. Efficiency, uniqueness and unpredictability needless to say, efficiency is the inability to frequently go to database queries to avoid duplication.
Moreover, the order number should be short enough to satisfy these conditions. Do not know the small partners in the daily project is also like me to think about the generation of the order of some small problems, you may also say that these things do not want to be so complicated, in fact, the small part is also agree with everyone's view, but we do the procedures are fastidious, and in the development of the order module, The position of the order number I believe we all know, so, when we write these small programs, we might as well take a few minutes to think about why this definition! OK, let's tell you how to generate the order!
first, the way we generate orders: You can use timestamps and random numbers, such as time (). rand (10000,99999); so it generates a 15-bit random number, the timestamp is accurate to the millisecond, and then five random number, but also to the high concurrency situation, The order number repeats, of course, we can also make the time stamp simple processing to become: Date ("Ymdhis"). Rand (10000,99999); In such a way, I believe that the small partners have also noticed that we have been using a Rand PHP random number function, so, When we go to the basics of PHP, when we come across random number functions, are we still wondering what the function is for? Now the little friends should understand it! Of course, we can also encapsulate it as a method, in case we use similar projects, but also improve the reusability of our daily code, so that the efficiency of our code has improved a lot, how to encapsulate it, small to write a simple small example:
function Get_sn () {
return date (' Ymdhis '). Rand (100000, 999999);
}
Of course, this is just a small example of a simple answer, more and better play the small program, or need your small partners to develop together!
thinkphp Generate order Number small case