PHP's standard library doesn't ship a UUID generator, but the ramsey/uuid Composer package fills that gap and has become the de facto standard for the job. Install it once, then call Uuid::uuid4() for a random, RFC 9562-compliant UUID anywhere in your codebase, whether that's an Eloquent model, a Symfony entity, or a plain PDO insert. Below you'll find copy-paste ready code for generating a PHP UUID, converting a string back into a UUID object, and the alternate uuid1()/uuid5() generation methods the same package provides.
How to Generate a UUID in PHP
The reliable way to generate a UUID in PHP is the ramsey/uuid package's Uuid::uuid4() method, which produces a random, cryptographically secure 128-bit identifier — PHP has no native function for this. Install it once with Composer and it works the same way in plain scripts, Laravel, Symfony, or any other framework. An import statement and a single function call are enough to generate uuid python with no pip install and no configuration.
composer require ramsey/uuid
<?php
require 'vendor/autoload.php';
use Ramsey\Uuid\Uuid;
$myUuid = Uuid::uuid4();
echo $myUuid->toString();
// Output: 550e8400-e29b-41d4-a716-446655440000
Explanation
composer require ramsey/uuidinstalls the package and registers its autoloader entry — no other setup required.use Ramsey\Uuid\Uuid;imports theUuidfactory class that exposes every generation method.Uuid::uuid4()returns aUuidInterfaceobject built from PHP's cryptographically securerandom_bytes(), not a plain string.->toString()(or casting with(string)) converts the object to the standard 36-character hyphenated format for output, JSON, or storage.
Convert a String to a UUID in PHP
When a UUID arrives as a string — from a request body, a form field, or a database row — parse it back into a proper UuidInterface object with Uuid::fromString(). In a web handler, parsing a string from a URL path or JSON body into a typed value is a core step of any go uuid service that stores records.
<?php
require 'vendor/autoload.php';
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
$uuidString = '550e8400-e29b-41d4-a716-446655440000';
try {
$uuid = Uuid::fromString($uuidString);
echo $uuid->toString();
} catch (InvalidUuidStringException $e) {
echo 'Invalid UUID format';
}
Explanation
Uuid::fromString()parses any valid UUID string — with or without hyphens, uppercase or lowercase — into aUuidInterfaceobject.- Malformed input throws
Ramsey\Uuid\Exception\InvalidUuidStringException, so wrap the call in atry/catchblock whenever the string comes from user input. - Two parsed
UuidInterfaceobjects compare equal with->equals()when they represent the same identifier, regardless of how each was created. - Use this pattern to validate incoming UUIDs from API requests or route parameters before trusting them.
Other Options for Generating UUIDs in PHP
uuid4() covers most use cases, but the same ramsey/uuid package also ships uuid1() and uuid5() for when you need a timestamp-based or deterministic identifier instead of a random one — no extra install required. PHP's PECL uuid extension offers a native uuid_create() function too, but it isn't enabled by default on most hosts, so ramsey/uuid stays the more portable choice.
<?php
use Ramsey\Uuid\Uuid;
// Version 1 - timestamp + node ID, sortable by creation time
$uuid1 = Uuid::uuid1();
// Version 5 - deterministic, same namespace + name always produce the same UUID
$uuid5 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'example.com');
Explanation
Uuid::uuid1()embeds the current timestamp and a node identifier, so IDs generated in sequence sort chronologically — useful for logs, but avoid it for public-facing IDs since it can leak host information.Uuid::uuid5($namespace, $name)hashes a namespace UUID and a name string with SHA-1, producing the exact same UUID every time for the same inputs.- Both methods ship in the same
ramsey/uuidpackage already installed foruuid4()— no extra Composer requirement. - Reach for
uuid5()over the legacy MD5-baseduuid3()when you need a deterministic, content-addressed identifier.