How to generate a new GUID?

mauzilla picture mauzilla · Feb 10, 2014 · Viewed 61.2k times · Source

I'm working on a web service which requires a new GUID() passed as a reference to a method within the service.

I am not familiar with C# or the GUID() object, but require something similar for PHP (so create a new object which from my understanding returns an empty/blank GUID).

Any ideas?

Answer

Michel Ayres picture Michel Ayres · Oct 2, 2014

You can try the following:

function GUID()
{
    if (function_exists('com_create_guid') === true)
    {
        return trim(com_create_guid(), '{}');
    }

    return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}

Source - com_create_guid