Generate UUIDs
use Webpatser\Uuid\Uuid; $uuid4 = Uuid::v4(); // Random (general use)$uuid7 = Uuid::v7(); // Time-ordered (databases)$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-ordered (RFC 9562 field order)$uuid8 = Uuid::generate(8); // Custom echo (string) $uuid4; // e.g. "550e8400-e29b-41d4-a716-446655440000"
v4() and v7() are shortcuts for generate(4) and generate(7). Every factory returns a Uuid object.
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, random2d25dff6-9d79-4527-8e71-0f0bfe454681 -
Uuid::v7()v7, time-ordered01a23838-7326-7003-865c-9ccac703e3ba -
Uuid::generate(1)v1, time and node309354f0-ee86-11f1-8dc8-9bf1e62e4e04 -
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)1f1ee863-0935-6620-bb23-9f860cff9c78 -
Uuid::generate(8)v8, customb704b485-e3d4-8327-80a3-92a83936a04d -
Uuid::nil()Nil00000000-0000-0000-0000-000000000000
The v3 and v5 values stay the same on every view; the others change each time.
Read a UUID
$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 (v1, v6 and v7 only, otherwise null)$uuid->node; // Hex node (v1 and v6 only, otherwise null)
On a v6 value generated by 3.x, time is not meaningful; see Upgrading from 3.x.
Import and validate
$valid = Uuid::validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); // true$valid = Uuid::validate('not-a-uuid'); // false $uuid = Uuid::import('6ba7b810-9dad-11d1-80b4-00c04fd430c8');echo $uuid->version; // 1 Uuid::compare('6BA7B810-9DAD-11D1-80B4-00C04FD430C8', (string) $uuid); // true
validate() only accepts the hyphenated 36-character form. import() is more lenient: it also accepts a urn:uuid: prefix, a form without hyphens and a 16-byte binary string. import() throws on input it cannot read, so validate first when the input comes from a user.
Name-based UUIDs
The same name and namespace always give the same UUID:
$a = Uuid::generate(5, 'example.com', Uuid::NS_DNS);$b = Uuid::generate(5, 'example.com', Uuid::NS_DNS); echo $a; // cfbff0d1-9375-5685-968c-48ce8b15ae17var_dump(Uuid::compare((string) $a, (string) $b)); // bool(true)
The predefined namespaces are Uuid::NS_DNS, Uuid::NS_URL, Uuid::NS_OID and Uuid::NS_X500. Any UUID string works as a namespace.
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()
0f870518-3e89-42c0-ab75-0f876ef9f6d7
Use the return value
All factory and query methods carry #[\NoDiscard]. PHP warns when you call one and ignore the result:
Uuid::v4(); // Warning: the return value should either be used or intentionally ignored by casting it as (void)(void) Uuid::v4(); // Intentionally ignored, no warning
Next steps
- Examples: complete scripts.
- API reference: every method and property.
- Performance: the built-in benchmark.