Generate UUID v4 and v7 in PHP
Copy-paste PHP code that produces a UUID v4 (random, the long-standing default) or UUID v7 (time-ordered, the modern choice for database keys). Both blocks below are production-ready.
UUID v4 in PHP
v4 is purely random — 122 bits of entropy, no structure. Use it when you don't want timing information embedded in the ID, or when you don't care about index locality.
<?php
// Composer:
// composer require ramsey/uuid
use Ramsey\Uuid\Uuid;
$id = Uuid::uuid4();
echo $id;
UUID v7 in PHP
v7 embeds a millisecond Unix timestamp in the first 48 bits, so the IDs sort chronologically. This is what modern databases want from a primary key — sequential inserts land in adjacent index pages instead of scattering writes.
<?php
// ramsey/uuid v4.7+ supports v7:
use Ramsey\Uuid\Uuid;
$id = Uuid::uuid7();
echo $id;
Library support
`ramsey/uuid` (v4.7+, October 2023) added v7. It's the dominant PHP UUID library — Symfony, Laravel, and most modern PHP frameworks depend on it directly.
Notes
PHP 8.4+ has `random_bytes()` built in, which `ramsey/uuid` uses internally. No separate setup needed.
Need them in bulk?
Our UUID generator produces 1 to 1000 UUIDs (v4 or v7) instantly, all in your browser, with one-click copy and JSON/CSV export. Useful for seeding test data or pre-generating IDs offline.