Home  >  Article  >  Backend Development  >  PHP implementation generates unique identifiers that are not repeated

PHP implementation generates unique identifiers that are not repeated

王林
王林forward
2020-01-23 19:15:383456browse

PHP implementation generates unique identifiers that are not repeated

You can use the PHP uniqid() function, which can be used to generate a unique identifier that is not repeated, based on the current timestamp in microseconds. However, in the case of high concurrency or extremely short intervals (such as loop code), a large amount of duplicate data will appear. Even if the second parameter is used, it will be repeated, and the best solution is to combine the md5 function to generate a unique ID.

Syntax:

string uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] )

Get a prefixed unique ID based on the number of microseconds in the current time. prefix Useful parameters.

For example: if on multiple hosts a unique ID may be generated in the same microsecond. If prefix is ​​empty, the length of the returned string is 13. If moreentropy is TRUE, the length of the returned string is 23. moreentropy If set to TRUE, uniqid() will add extra entropy to the end of the returned string (using a combined linear congruential generator). Make the unique ID more unique.

(Free learning video tutorial sharing: php video tutorial)

Method 1:

This method will generate a large number of For repeated data, run the following PHP code and the array index will be the unique identifier generated, and the corresponding element value is the number of times the unique identifier is repeated.

PHP implementation generates unique identifiers that are not repeated

Method 2:

The amount of duplicate unique identifiers generated by this method is significantly reduced.

PHP implementation generates unique identifiers that are not repeated

Method 3:

There are no duplicates in the unique identifier generated by this method.

PHP implementation generates unique identifiers that are not repeated

Recommended related articles and tutorials: php tutorial

The above is the detailed content of PHP implementation generates unique identifiers that are not repeated. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete