Skip to content

Instantly share code, notes, and snippets.

@dchest
Created February 15, 2020 19:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dchest/e77e705ef2c120c262b58a7a4893df61 to your computer and use it in GitHub Desktop.
Save dchest/e77e705ef2c120c262b58a7a4893df61 to your computer and use it in GitHub Desktop.
UUIDv4 generator for PHP 7+
<?php
/**
* Generate a random UUIDv4.
* @return string UUID
*/
function generate_uuid()
{
$b = random_bytes(16);
$b[6] = chr(ord($b[6]) & 0x0f | 0x40);
$b[8] = chr(ord($b[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($b), 4));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment