Use the uniqid () function in php to generate a unique id.

Source: Internet
Author: User

Use 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)

The uniqid () function generates a unique ID based on the current time in microseconds. Prefix is optional. It is the prefix specified by ID. This parameter is useful if two scripts generate IDS exactly in the same microsecond. More_entropy is optional. Specify more entropy at the end of the returned value. If the prefix parameter is null, the returned string contains 13 characters in length. If the more_entropy parameter is set to true, it is a string of 23 characters. If the more_entropy parameter is set to true, an extra entropy is added at the end of the returned value (the program is generated using the combination of linear and remainder values), which improves the uniqueness of the result. The Return Value Returns a unique identifier in the form of a string. Tip and comment: because the system time is used, the ID generated by this function is not optimal. To generate an absolutely unique ID, use the md5 () function (find it in the string function reference) // mt_rand () to return a random integer using the Mersenne Twister algorithm. Mt_rand (min, max) indicates that 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 (including 5 and 15), use mt_rand (5, 15 ). Many old libc random number generators have some uncertain and unknown features and are very slow. The rand () function of PHP uses the libc random number generator by default. The mt_rand () function is informal to replace it. This function uses known features in Mersenne Twister as a random number generator, which can generate random values at an average speed four times faster than the rand () provided by libc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.