All functionality lives in Webpatser\Uuid\Uuid.
Generate UUIDs
use Webpatser\Uuid\Uuid; $uuid4 = Uuid::v4(); // Random$uuid7 = Uuid::v7(); // Unix timestamp + random, time-ordered$uuid1 = Uuid::generate(1); // Time-based$uuid3 = Uuid::generate(3, 'example.com', Uuid::NS_DNS); // Name-based (MD5)$uuid5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS); // Name-based (SHA-1)$uuid6 = Uuid::generate(6); // Time-based, version 6$uuid8 = Uuid::generate(8); // Custom echo (string) $uuid4;
Version 2 is not supported and throws an Exception.
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.
-
Uuid::v4()v4, randomcd59b8f1-1757-4bd0-9c33-a57228c62959 -
Uuid::v7()v7, time-ordered01a23843-3952-7004-a1e9-bc0bb7a87f46 -
Uuid::generate(1)v1, time and noded5705810-ee87-11f1-b180-9d2e19a53a78 -
Uuid::generate(3, 'example.com', Uuid::NS_DNS)v3, MD5 name-based9073926b-929f-31c2-abc9-fad77ae3e8eb -
Uuid::generate(5, 'example.com', Uuid::NS_DNS)v5, SHA-1 name-basedcfbff0d1-9375-5685-968c-48ce8b15ae17 -
Uuid::generate(6)v6, time-ordered (RFC 9562)1f1ee87d-5705-6880-8ccb-475c3215cb28 -
Uuid::generate(8)v8, customc7c758dd-2fe3-86d9-9bbc-dca616f493fe -
Uuid::nil()Nil00000000-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.
Choose a version
| Version | Source | Use |
|---|---|---|
| 1 | Gregorian time plus a node (random if none is given) | Time-based identifiers |
| 3 | MD5 of namespace and name | Deterministic IDs |
| 4 | Random | General purpose |
| 5 | SHA-1 of namespace and name | Deterministic IDs |
| 6 | Gregorian timestamp and node, version nibble 6 (at 2.0.1 the timestamp bytes use the same layout as version 1) | Time-based identifiers |
| 7 | Unix millisecond timestamp plus a per-process sequence and random bits | Database keys |
| 8 | Custom data, or random | Application-specific formats |
Read properties
$uuid = Uuid::v7(); $uuid->string; // "019d05e8-dfa1-7009-a8f7-4e1c868ccfa4"$uuid->hex; // "019d05e8dfa17009a8f74e1c868ccfa4"$uuid->bytes; // 16-byte binary string$uuid->urn; // "urn:uuid:019d05e8-dfa1-7009-a8f7-4e1c868ccfa4"$uuid->version; // 7$uuid->variant; // 1 (RFC 4122)$uuid->time; // Unix timestamp as float (versions 1, 6 and 7 only, otherwise null)$uuid->node; // MAC address hex (versions 1 and 6 only, otherwise null)
Validate, import and compare
$valid = Uuid::validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); // true$valid = Uuid::validate('not-a-uuid'); // false $uuid = Uuid::import('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); $same = Uuid::compare( '6BA7B810-9DAD-11D1-80B4-00C04FD430C8', '6ba7b810-9dad-11d1-80b4-00c04fd430c8',); // true, case-insensitive
validate() accepts only the hyphenated 36-character form. compare() returns false when either value is not a valid UUID.
Name-based UUIDs
$a = Uuid::generate(5, 'example.com', Uuid::NS_DNS);$b = Uuid::generate(5, 'example.com', Uuid::NS_DNS); // $a and $b are identical: cfbff0d1-9375-5685-968c-48ce8b15ae17
The namespace can be one of Uuid::NS_DNS, Uuid::NS_URL, Uuid::NS_OID, Uuid::NS_X500, or any UUID string.
Try it live
Pick a version and, for v3 and v5, a name and a namespace. The generator runs the matching Uuid::generate() call on this server and lets you copy the result.
Uuid::v4()
95db2153-1fb9-456c-a01f-e52b29bb41bc
This demo runs webpatser/uuid 3.0.0, the version installed on this site.
Ignored results
Factory and query methods carry #[\NoDiscard]. PHP emits a warning when you call one and discard the result.