Home > Backend Development > PHP Tutorial > Use uniqid() function to generate unique id in php_PHP tutorial

Use uniqid() function to generate unique id in php_PHP tutorial

WBOY
Release: 2016-07-13 16:53:55
Original
1173 people have browsed it

Use uniqid() function in php to generate unique id

function createId($prefix = "")
{
    $str = md5(uniqid(mt_rand(), true));
    return $prefix . $str;
}

//
uniqid(prefix,more_entropy)
Copy after login

The uniqid() function generates a unique ID based on the current time in microseconds.
prefix Optional. Specifies the prefix for the ID. This parameter is useful if two scripts happen to generate IDs at the same microsecond.
more_entropy Optional. Specifies more entropy at the end of the return value.
If the prefix parameter is empty, the returned string is 13 strings long. If the more_entropy parameter is set to true, it is 23 strings long.
If the more_entropy parameter is set to true, additional entropy is added at the end of the return value (using the combined linear congruence generator), which makes the result more unique.
Return value
Returns the unique identifier as a string.
Tips and Notes
Note: Since it is based on system time, the ID generated by this function is not optimal. To generate an absolutely unique ID, use the md5() function (find it in the String Functions Reference)
//
mt_rand() returns a random integer using the Mersenne Twister algorithm.
mt_rand(min,max)
Description
If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random number between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (inclusive), use mt_rand(5, 15).
Many old libc random number generators have some uncertain and unknown characteristics and are slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features of Mersenne Twister as a random number generator, which can generate random values ​​on average four times faster than rand() provided by libc.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1053348.htmlTechArticleUse the uniqid() function in php to generate a unique id function createId($prefix = ){ $str = md5( uniqid(mt_rand(), true)); return $prefix . $str;}//uniqid(prefix,more_entropy) uniqid() function base...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template