~/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 2.0.1
You are reading the 2.x documentation
The current line is 4.x. Read this page for 4.x

UUID Library 2.x

Examples

Complete, copyable scripts for generating, inspecting and comparing UUIDs, plus a live validator.

The scripts on this page are the files in the examples/ directory of the repository at v2.0.1. They expect vendor/autoload.php one directory up.

Live validator

The demo below runs the UUID version installed on this site, not necessarily 2.x.

⚡ 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

d3383539-f9f0-4c99-b3f8-be87728974e0
d3383539f9f04c99b3f8be87728974e0
d3383539f9f04c99b3f8be87728974e0

🔍 UUID Validator & Inspector

Enter a UUID above to validate

Basic usage

examples/basic-usage.php: generate every version, import, validate, read properties, use the nil UUID.

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
echo "=== Basic UUID Generation ===\n";
 
// Generate a v4 UUID (random)
$uuid4 = Uuid::v4();
echo "UUID v4: " . (string) $uuid4 . "\n";
 
// Generate a v7 UUID (time-ordered, recommended for databases)
$uuid7 = Uuid::v7();
echo "UUID v7: " . (string) $uuid7 . "\n";
 
// Generate 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";
 
echo "\n=== Import & Validate ===\n";
 
// 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 "\nIs valid UUID: " . (Uuid::validate($valid) ? 'yes' : 'no') . "\n";
 
$invalid = 'not-a-uuid';
echo "Is valid UUID: " . (Uuid::validate($invalid) ? 'yes' : 'no') . "\n";
 
echo "\n=== UUID Properties ===\n";
 
$uuid = Uuid::generate(1);
echo "String: " . (string) $uuid . "\n";
echo "Hex: " . $uuid->hex . "\n";
echo "URN: " . $uuid->urn . "\n";
echo "Node: " . $uuid->node . "\n";
 
echo "\n=== Nil UUID ===\n";
 
$nil = Uuid::nil();
echo "Nil UUID: " . (string) $nil . "\n";
echo "Is nil: " . ($nil->isNil() ? 'yes' : 'no') . "\n";

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
    1cab2869-c840-4ce1-80b6-2ca9110c4f77
  • Uuid::v7() v7, time-ordered
    01a23843-35ab-700a-9dc7-149574764e66
  • Uuid::generate(1) v1, time and node
    d4e1c680-ee87-11f1-8fbf-e1c3b6c50ae3
  • 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)
    1f1ee87d-4e1c-66e0-a0c5-3f06c7735362
  • Uuid::generate(8) v8, custom
    41d5dff4-be64-87b3-b6f3-629b061323c5
  • Uuid::nil() Nil
    00000000-0000-0000-0000-000000000000

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

This demo runs webpatser/uuid 3.0.0, the version installed on this site.

Time-based UUIDs

examples/time-based.php: ordering, comparison and time extraction for versions 1, 6 and 7.

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
echo "=== Time-Based UUIDs ===\n";
 
// Generate multiple v7 UUIDs to show time ordering
echo "Generating 5 time-ordered v7 UUIDs:\n";
 
for ($i = 1; $i <= 5; $i++) {
$uuid = Uuid::v7();
echo "UUID #{$i}: " . (string) $uuid . "\n";
 
// Small delay to show time progression
usleep(1000); // 1ms
}
 
echo "\n=== UUID Comparison ===\n";
 
$uuid1 = Uuid::v7();
$uuid2 = Uuid::v7();
 
echo "UUID 1: " . (string) $uuid1 . "\n";
echo "UUID 2: " . (string) $uuid2 . "\n";
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";
 
echo "\n=== Time Extraction ===\n";
 
$v1 = Uuid::generate(1);
echo "V1 UUID: " . (string) $v1 . "\n";
echo "V1 Time: " . $v1->time . "\n";
 
$v6 = Uuid::generate(6);
echo "V6 UUID: " . (string) $v6 . "\n";
echo "V6 Time: " . $v6->time . "\n";
 
$v7 = Uuid::v7();
echo "V7 UUID: " . (string) $v7 . "\n";
echo "V7 Time: " . $v7->time . "\n";
echo "V7 DateTime: " . date('Y-m-d H:i:s', (int) $v7->time) . "\n";

Benchmark

examples/benchmark.php: measure every version, and compare against ramsey/uuid when it is installed.

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
echo "=== UUID Benchmark ===\n";
echo "PHP " . PHP_VERSION . " on " . php_uname('s') . " " . php_uname('m') . "\n\n";
 
$iterations = (int) ($argv[1] ?? 10000);
echo "Running {$iterations} iterations per version...\n\n";
 
$versions = [1, 3, 4, 5, 6, 7, 8];
 
printf("%-10s %12s %12s %16s\n", 'Version', 'Total (ms)', 'Avg (µs)', 'UUIDs/sec');
echo str_repeat('-', 54) . "\n";
 
foreach ($versions as $version) {
$result = Uuid::benchmark($iterations, $version);
printf(
"%-10s %12.2f %12.3f %16s\n",
"V{$version}",
$result['total_time_ms'],
$result['avg_time_us'],
number_format($result['uuids_per_second']),
);
}
 
echo "\n";
 
// Compare with ramsey/uuid if available
if (class_exists(\Ramsey\Uuid\Uuid::class)) {
echo "=== Comparison with ramsey/uuid ===\n\n";
 
$start = hrtime(true);
for ($i = 0; $i < $iterations; $i++) {
\Ramsey\Uuid\Uuid::uuid4();
}
$ramseyV4 = (hrtime(true) - $start) / 1_000_000;
 
$start = hrtime(true);
for ($i = 0; $i < $iterations; $i++) {
\Ramsey\Uuid\Uuid::uuid7();
}
$ramseyV7 = (hrtime(true) - $start) / 1_000_000;
 
$webpatserV4 = Uuid::benchmark($iterations, 4)['total_time_ms'];
$webpatserV7 = Uuid::benchmark($iterations, 7)['total_time_ms'];
 
printf("%-20s %12s %12s\n", '', 'webpatser', 'ramsey');
echo str_repeat('-', 46) . "\n";
printf("%-20s %10.2f ms %10.2f ms\n", "V4 ({$iterations}x)", $webpatserV4, $ramseyV4);
printf("%-20s %10.2f ms %10.2f ms\n", "V7 ({$iterations}x)", $webpatserV7, $ramseyV7);
} else {
echo "Install ramsey/uuid to compare: composer require --dev ramsey/uuid\n";
}

Run it with php examples/benchmark.php 10000.

Next steps