PHP · Ramsey/UUID · Code Reference

Generate a UUID in PHP

Complete reference for generating UUIDs in PHP — covers the Ramsey/Uuid package (the PHP standard), a zero-dependency native fallback, validation, and Laravel/Symfony integration.

Quick Start

Generate a Random UUID (v4) with Ramsey/Uuid

PHP has no built-in UUID function. The ramsey/uuid package is the de facto standard, used by Laravel, Symfony, and most PHP frameworks:

Terminal
composer require ramsey/uuid
PHP
use Ramsey\Uuid\Uuid; $uuid = Uuid::uuid4(); echo $uuid->toString(); // 550e8400-e29b-41d4-a716-446655440000
💡

ramsey/uuid is already in your project

If you use Laravel, ramsey/uuid is already a dependency — Laravel’s Str::uuid() helper uses it internally. No extra install needed for Laravel projects.

Zero-Dependency Option

Generate a UUID in PHP Without Composer

If you can’t install a package, PHP 7+’s random_bytes() lets you build an RFC-compliant UUID v4 manually:

PHP 7+ (no dependencies)
function generateUuidV4(): string { $data = random_bytes(16); $data[6] = chr((ord($data[6]) & 0x0f) | 0x40); // version 4 $data[8] = chr((ord($data[8]) & 0x3f) | 0x80); // variant return vsprintf(‘%s%s-%s-%s-%s-%s%s%s’, str_split(bin2hex($data), 4)); } echo generateUuidV4();

All Versions

UUID Versions with Ramsey/Uuid

VersionPHP CallUse Case
v1 (Time-based)Uuid::uuid1()Legacy systems. Leaks MAC address — avoid in user-facing apps.
v3 (MD5 name-based)Uuid::uuid3($ns, $name)Deterministic. Use v5 instead (MD5 is broken).
v4 (Random)Uuid::uuid4()General purpose. Default choice.
v5 (SHA-1 name-based)Uuid::uuid5($ns, $name)Deterministic — same input always gives same UUID.
v7 (Unix timestamp)Uuid::uuid7() (v4.7+)Time-ordered. Recommended for database primary keys in 2026.
PHP — UUID v7 for database keys
use Ramsey\Uuid\Uuid; $uuid = Uuid::uuid7(); echo $uuid->toString(); // time-ordered, sortable UUID

Validation

Validate a UUID String in PHP

PHP — regex (no dependency)
function isValidUuid(string $uuid): bool { return (bool) preg_match( ‘/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i’, $uuid ); } isValidUuid(‘550e8400-e29b-41d4-a716-446655440000’); // true isValidUuid(‘not-a-uuid’); // false
PHP — with Ramsey/Uuid
use Ramsey\Uuid\Uuid; Uuid::isValid(‘550e8400-e29b-41d4-a716-446655440000’); // true

Frameworks

UUIDs in Laravel & Symfony

Laravel — migration
Schema::create(‘orders’, function (Blueprint $table) { $table->uuid(‘id’)->primary(); $table->timestamps(); });
Laravel — Eloquent model
use Illuminate\Database\Eloquent\Concerns\HasUuids; class Order extends Model { use HasUuids; } // Or generate manually: $id = Str::uuid()->toString();
Symfony — Doctrine entity
use Symfony\Bridge\Doctrine\Types\UuidType; use Symfony\Component\Uid\Uuid; #[ORM\Column(type: UuidType::NAME, unique: true)] private Uuid $id;

FAQ

PHP UUID FAQ

No. Unlike Python or Java, PHP has no native UUID generation function in its standard library. The ramsey/uuid Composer package is the de facto standard used by Laravel, Symfony, and most PHP projects. For zero-dependency generation, you can manually build a UUID v4 using random_bytes(16) with the correct version and variant bits set.
Yes. Laravel includes Str::uuid() out of the box, which internally uses the ramsey/uuid package (already a Laravel dependency) to generate a UUID v4. Laravel also provides the HasUuids trait for Eloquent models to automatically use UUIDs as primary keys instead of auto-incrementing integers.
Install ramsey/uuid version 4.7 or later, then call Uuid::uuid7(). This produces a time-ordered UUID with a Unix millisecond timestamp prefix, recommended for database primary keys in 2026 because it improves B-tree index performance compared to fully random UUID v4 keys.
Yes, as long as you use random_bytes() rather than the weaker rand() or mt_rand() functions. random_bytes() is PHP’s cryptographically secure random source since PHP 7.0. The manual approach shown above produces a fully RFC 4122-compliant UUID v4 with correct version and variant bits.

Need to Generate a UUID Right Now?

Skip the code — use our free browser-based generator for all 8 UUID versions.

Open UUID Generator