Sharing UUID generation udfs in PHP
UUID is the full name of Universally unique identifier. It is an identifier that can be generated by any computer and can be hardly duplicated without a central database for management. The value range of UUID is large. It is said that a UUID is allocated to every grain of sand in the world, and there will be no repetition.
UUID is required for WordPress code modification recently. However, PHP does not generate a UUID function, so you have to write one by 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, 0 xffff), mt_rand (0, 0 xffff ), Mt_rand (0, 0 xffff ), Mt_rand (0, 0x0fff) | 0x4000, Mt_rand (0, 0x3fff) | 0x8000, Mt_rand (0, 0 xffff), mt_rand (0, 0 xffff), mt_rand (0, 0 xffff) ); } } |
The above code generates a UUID version 4. UUID currently has five versions. The fourth version is completely random and is easy to generate. Among them, com_create_guid is a function of PHP in Windows. It directly calls the CreateGuid function of COM to generate UUID, but there is no corresponding function library in Linux, so you have to write it yourself. To facilitate use on different platforms, a function with the same name is created. Other codes generate random numbers.
For usage, you can directly call com_create_guid.