~/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

Binary UUIDs

Read and write UUIDs as 16 raw bytes or plain hex with the bytes, hex and import APIs.

A UUID is 128 bits. The library keeps it as a 16-byte string internally and exposes that form, so you can store 16 bytes instead of 36 characters.

To binary and hex

use Webpatser\Uuid\Uuid;
 
$uuid = Uuid::v7();
 
$binary = $uuid->bytes; // 16 raw bytes
$hex = $uuid->hex; // 32 hex characters, no hyphens
$string = $uuid->string; // 36 characters
 
strlen($binary); // 16

Binary versus string size

The demo generates 1000 UUIDs with Uuid::v4() and adds up strlen() of the string form and of the binary form: 36,000 bytes against 16,000 bytes. The timings are measured on this server.

1,000 UUIDs from Uuid::v4(), measured on this server just now

String, $uuid->string

36,000 bytes

read in 0.052 ms

Binary, $uuid->bytes

16,000 bytes

read in 0.045 ms

Saved by binary

55.6%

20,000 bytes

First string: e0c24b9c-db26-4409-97c7-2e65ba985dd1
Same bytes as hex: e0c24b9cdb26440997c72e65ba985dd1

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

From binary and hex

import() accepts 16 raw bytes, hex without hyphens, hyphenated strings and urn:uuid: strings.

$fromBinary = Uuid::import($binary);
$fromHex = Uuid::import('6ba7b8109dad11d180b400c04fd430c8');
$fromUrn = Uuid::import('urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8');
 
echo $fromBinary->string === $uuid->string ? 'same' : 'different'; // same

Input that is neither 16 bytes nor 32 hex digits (after stripping urn:uuid: and non-hex characters) throws a TypeError. Any 16-byte string is accepted as raw bytes. validate() only accepts the hyphenated form, so it cannot be used to check binary or plain hex input.

Time-ordered hex

uuid_ordered returns 16 hex characters: the time fields only, reordered (time high, time mid, time low). It is not a full UUID and cannot be imported back.

$uuid = Uuid::generate(1);
 
echo $uuid->uuid_ordered;

Comparing

compare() normalizes both sides before comparing, so binary and string forms can be mixed:

Uuid::compare($uuid->bytes, $uuid->string); // true

SQL Server

SQL Server uses a different byte order. Use toSqlServerBinary() and importFromSqlServer() instead of bytes and import() there: Microsoft SQL Server.