~/docs
Resonate
Resonate
4.x 4.0.0, current 3.x 3.0.1 2.x 2.0.1 1.x 1.3.0
UUID Library
Toggle sidebar
UUID Library
webpatser/uuid 4.0.0

UUID Library 4.x

Examples

Complete, copyable examples for generating, importing, comparing and inspecting UUIDs with webpatser/uuid, plus a live validator.

The live validator below uses the same Uuid::validate() and Uuid::import() calls as the code on this page.

⚡ Quick Actions

This interactive experience is powered by Laravel, Livewire & PHP, using the actual UUID library for real-time generation and validation

🎲 Interactive UUID Generator

3fa47ec8-82a1-458b-a6f6-f23f2f4eed7e
3fa47ec882a1458ba6f6f23f2f4eed7e
3fa47ec882a1458ba6f6f23f2f4eed7e

🔍 UUID Validator & Inspector

Enter a UUID above to validate

The scripts below follow examples/basic-usage.php and examples/time-based.php in the repository. Run them from the package root after composer install.

Generate every version

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
// Random
$uuid4 = Uuid::v4();
echo "UUID v4: " . (string) $uuid4 . "\n";
 
// Time-ordered, recommended for databases
$uuid7 = Uuid::v7();
echo "UUID v7: " . (string) $uuid7 . "\n";
 
// Other versions
$uuid1 = Uuid::generate(1);
echo "UUID v1: " . (string) $uuid1 . "\n";
 
$uuid3 = Uuid::generate(3, 'example.com', Uuid::NS_DNS);
echo "UUID v3: " . (string) $uuid3 . "\n";
 
$uuid5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS);
echo "UUID v5: " . (string) $uuid5 . "\n";
 
$uuid6 = Uuid::generate(6);
echo "UUID v6: " . (string) $uuid6 . "\n";
 
$uuid8 = Uuid::generate(8);
echo "UUID v8: " . (string) $uuid8 . "\n";

The v3 and v5 lines always print 9073926b-929f-31c2-abc9-fad77ae3e8eb and cfbff0d1-9375-5685-968c-48ce8b15ae17. The v6 line is time-ordered (RFC 9562 field order).

Live output

The block below generates one UUID per version on this server, each next to the call that produced it. Press Generate again for new values. The name-based rows are always the same for the same name and namespace.

Generated on this server for this page view
  • Uuid::v4() v4, random
    c01bbea7-8a2f-4691-b60e-6e4183032edf
  • Uuid::v7() v7, time-ordered
    01a23838-7139-7006-a615-72bd3aa10395
  • Uuid::generate(1) v1, time and node
    304804d0-ee86-11f1-b99e-79a16e147346
  • Uuid::generate(3, 'example.com', Uuid::NS_DNS) v3, MD5 name-based
    9073926b-929f-31c2-abc9-fad77ae3e8eb
  • Uuid::generate(5, 'example.com', Uuid::NS_DNS) v5, SHA-1 name-based
    cfbff0d1-9375-5685-968c-48ce8b15ae17
  • Uuid::generate(6) v6, time-ordered (RFC 9562)
    1f1ee863-0480-6530-a919-a3aa7b2ee849
  • Uuid::generate(8) v8, custom
    5ee5100d-e49d-84ff-9d70-b2fe28db3c5a
  • Uuid::nil() Nil
    00000000-0000-0000-0000-000000000000

The v3 and v5 values stay the same on every view; the others change each time.

Import, validate and inspect

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
// Import an existing UUID string
$imported = Uuid::import('6ba7b810-9dad-11d1-80b4-00c04fd430c8');
echo "Imported: " . (string) $imported . "\n";
echo "Version: " . $imported->version . "\n";
echo "Variant: " . $imported->variant . "\n";
 
// Validate UUID strings
$valid = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
echo "Is valid UUID: " . (Uuid::validate($valid) ? 'yes' : 'no') . "\n";
 
$invalid = 'not-a-uuid';
echo "Is valid UUID: " . (Uuid::validate($invalid) ? 'yes' : 'no') . "\n";
 
// Properties of a v1 UUID
$uuid = Uuid::generate(1);
echo "String: " . (string) $uuid . "\n";
echo "Hex: " . $uuid->hex . "\n";
echo "URN: " . $uuid->urn . "\n";
echo "Node: " . $uuid->node . "\n";
 
// Nil UUID
$nil = Uuid::nil();
echo "Nil UUID: " . (string) $nil . "\n";
echo "Is nil: " . ($nil->isNil() ? 'yes' : 'no') . "\n";

Time-ordered UUIDs and time extraction

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
// Five v7 UUIDs, one millisecond apart
for ($i = 1; $i <= 5; $i++) {
$uuid = Uuid::v7();
echo "UUID #{$i}: " . (string) $uuid . "\n";
 
usleep(1000); // 1ms
}
 
// Comparison
$uuid1 = Uuid::v7();
$uuid2 = Uuid::v7();
 
echo "Are equal: " . (Uuid::compare((string) $uuid1, (string) $uuid2) ? 'yes' : 'no') . "\n";
echo "UUID 1 == UUID 1: " . (Uuid::compare((string) $uuid1, (string) $uuid1) ? 'yes' : 'no') . "\n";
 
// Time extraction (v1, v6 and v7)
$v1 = Uuid::generate(1);
echo "V1 Time: " . $v1->time . "\n";
 
$v6 = Uuid::generate(6);
echo "V6 Time: " . $v6->time . "\n";
 
$v7 = Uuid::v7();
echo "V7 Time: " . $v7->time . "\n";
echo "V7 DateTime: " . date('Y-m-d H:i:s', (int) $v7->time) . "\n";

Deterministic IDs under your own namespace

Derive a namespace UUID once, then use it for every name. The namespace argument is a string, so pass ->string.

use Webpatser\Uuid\Uuid;
 
$appNamespace = Uuid::generate(5, 'myapp.com', Uuid::NS_DNS)->string;
 
$userIds = [];
foreach (['alice', 'bob', 'charlie'] as $username) {
$userIds[$username] = Uuid::generate(5, $username, $appNamespace);
}

Next steps