Skip to content

Instantly share code, notes, and snippets.

@RobinvanderVliet
Created June 13, 2023 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobinvanderVliet/39681e7edf34a2c96ca1499232bd091a to your computer and use it in GitHub Desktop.
Save RobinvanderVliet/39681e7edf34a2c96ca1499232bd091a to your computer and use it in GitHub Desktop.
Universally Unique IDentifier (UUID) version 4 in PHP
<?php
function uuid(): string
{
$data = random_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment