uuid

UUID v4, UUID v7, NIL UUID and ULID generator, validator and formatter.

v1.0.0Math, time & IDsbundledimport "uuid";
Overview Installation Documentation Examples

Overview

Zero-dependency HolyPHP library for generating, validating, inspecting, and formatting UUIDs (v4, v7, NIL, MAX) and ULIDs.

bundled — works offline zero dependencies ulid guid

Quick start

import "uuid";

$id4 = uuid_v4();
println("UUID v4: " . $id4);

$id7 = uuid_v7();
println("UUID v7: " . $id7);
println("v7 timestamp (ms): " . uuid_timestamp_ms($id7));

$ulid = uuid_ulid();
println("ULID: " . $ulid);

if (uuid_is_valid($id4)) {
    println("Version: " . uuid_version($id4));
    println("URN: " . uuid_format($id4, "urn"));
}

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install uuid

This adds the library to hphp.yaml, installs it into .hphp/packages/uuid/ and pins the exact version in hphp.lock:

require:
  uuid: "^1.0"

Use it

import "uuid";

uuid is bundled with the compiler, so import "uuid"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install uuid          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install uuid
Packageuuid
Version1.0.0 (follows the compiler)
Entry fileuuid.hphp
Sourcepackages/lib/uuid ↗

Documentation

API Reference

Generators

Validation & Inspection

Normalization & Formatting

Error Handling

All validation, extraction, and formatting functions throw Exception with clear, prefixed messages on invalid input:

try {
    uuid_version("not-a-uuid");
} catch (Exception $e) {
    // "uuid: invalid UUID length in 'not-a-uuid' (expected 32 hex chars or 36-char canonical UUID)"
    println($e->getMessage());
}

Common error cases:

Examples

01Quick start

Bundled with the compiler — import "uuid"; works offline in any file, or add it to a project with hphp install uuid.

import "uuid";

$id4 = uuid_v4();
println("UUID v4: " . $id4);

$id7 = uuid_v7();
println("UUID v7: " . $id7);
println("v7 timestamp (ms): " . uuid_timestamp_ms($id7));

$ulid = uuid_ulid();
println("ULID: " . $ulid);

if (uuid_is_valid($id4)) {
    println("Version: " . uuid_version($id4));
    println("URN: " . uuid_format($id4, "urn"));
}
Try it
hphp install uuid
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_uuid.hphp.