Generate UUID Custom function sharing in PHP

Source: Internet
Author: User
Tags 04x uuid

The UUID full name is the universally unique identifier, which is an identifier that can be generated using any computer, without requiring a central database for management, which guarantees that there is little chance of duplication. The range of the UUID is said to assign a UUID to every grain of sand in the world, and there is no repetition.

Recently in the code to change WordPress, need to use UUID. However, there is no function in PHP to generate the UUID, so you have to write one yourself.

1 2 3 4 5 6 7 8 9 10 11 if (!function_exists (' Com_create_guid ')) {function Com_create_guid () {return sprintf ('%04x%04x-%04x-%04x-%04x-%04x %04x%04x ', Mt_rand (0, 0xFFFF), Mt_rand (0, 0xFFFF), Mt_rand (0, 0xFFFF), Mt_rand (0, 0X0FFF) | 0x4000, Mt_rand (0, 0X3FFF) | 0x8000, Mt_rand (0, 0xFFFF), Mt_rand (0, 0xFFFF), Mt_rand (0, 0xFFFF)); } }

The above code can generate a UUID version 4. The UUID currently has 5 versions, of which the fourth edition is completely random and easy to generate. And the Com_create_guid, is a function of PHP in Windows, it directly calls the COM CreateGUID function to generate the UUID, but in Linux does not have the corresponding function library, had to write their own. To facilitate use on different platforms, a function with the same name is created. The other code is to generate random numbers.

As for usage, call Com_create_guid () directly.

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.