PHP Generate a Globally Unique Identifier (GUID) in PHP

Learn how to generate a globally unique identifier (GUID) in PHP using either a native function or a custom method. Get a randomly generated GUID that can be used for various purposes.

function createGUID()
{
    static $_isNativeFn;

    if ($_isNativeFn || function_exists('com_create_guid')) {
        $_isNativeFn = true;
        return 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)
    );
}