How to Generate UUIDs in PHP

Generate UUIDs in PHP with a library such as ramsey/uuid when you need application-level UUID v4 and UUID v7 support.

Recommendation: use UUID v4 for simple random identifiers, and UUID v7 when you want chronological ordering in APIs, queues, or database inserts.

How to generate UUID v4 in PHP

// composer require ramsey/uuid
use Ramsey\Uuid\Uuid;

$id = Uuid::uuid4();
echo $id->toString();

How to generate UUID v7 in PHP

// composer require ramsey/uuid
use Ramsey\Uuid\Uuid;

$id = Uuid::uuid7();
echo $id->toString();

Native support notes

  • PHP ecosystems generally say UUID, not GUID.
  • Most real-world PHP code uses a package rather than a core built-in function.
  • Keep library version requirements explicit when documenting UUID v7.

Practical notes

  • Use Composer-managed dependencies so all services generate UUIDs the same way.
  • UUID v4 is widely interoperable.
  • UUID v7 is usually a better fit for write-heavy database workloads.

Frequently Asked Questions

In most PHP applications, UUID v7 comes from a library rather than a universal core API.

ramsey/uuid is one of the most established and widely used options in PHP projects.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.