Skip to content

Generate UUID in PHP

PHP doesn't have a built-in UUID function, but you can generate a simple unique identifier using uniqid(). However, for proper UUID generation, use the ramsey/uuid library.

php
<?php
// Simple unique ID (not a proper UUID)
$simpleId = uniqid();
echo "Simple ID: " . $simpleId;

// For proper UUIDs, use ramsey/uuid library
require_once 'vendor/autoload.php';
use Ramsey\Uuid\Uuid;

$uuid4 = Uuid::uuid4();
echo "UUID v4: " . $uuid4->toString();
?>

For generating specific UUID versions, use the ramsey/uuid library which offers comprehensive UUID support:

php
<?php
require_once 'vendor/autoload.php';
use Ramsey\Uuid\Uuid;

// Generate UUID version 1 - time based
$uuid1 = Uuid::uuid1();
echo "UUID v1: " . $uuid1->toString() . "\n";

// Generate UUID version 3 - deterministic and hashed with MD5
$namespace = Uuid::NAMESPACE_URL;
$uuid3 = Uuid::uuid3($namespace, 'https://example.com');
echo "UUID v3: " . $uuid3->toString() . "\n";

// Generate UUID version 4 - random
$uuid4 = Uuid::uuid4();
echo "UUID v4: " . $uuid4->toString() . "\n";

// Generate UUID version 5 - deterministic and hashed with SHA-1
$uuid5 = Uuid::uuid5($namespace, 'https://example.com');
echo "UUID v5: " . $uuid5->toString() . "\n";

// Generate UUID version 6 - time-ordered
$uuid6 = Uuid::uuid6();
echo "UUID v6: " . $uuid6->toString() . "\n";

// Generate UUID version 7 - time-ordered with random
$uuid7 = Uuid::uuid7();
echo "UUID v7: " . $uuid7->toString() . "\n";
?>

Installation & Setup

Composer Installation:

bash
composer require ramsey/uuid

Laravel Integration: Laravel includes ramsey/uuid by default in newer versions. You can use it directly:

php
use Illuminate\Support\Str;

$uuid = Str::uuid();
// or
$uuid = Str::orderedUuid(); // Time-ordered for better database performance

Framework Integration

Symfony:

php
use Symfony\Component\Uid\Uuid;

$uuid = Uuid::v4();
$uuid = Uuid::v6(); // Time-ordered

WordPress:

php
// Add to functions.php or plugin
function generate_uuid() {
    return Uuid::uuid4()->toString();
}

UUID Version Comparison

Choose the right version for your PHP application:

  • Version 1 - Time-based, includes MAC address
  • Version 3 - MD5 namespace-based, deterministic
  • Version 4 - Random, most popular choice
  • Version 5 - SHA-1 namespace-based, more secure than v3
  • Version 6 - Time-ordered, better than v1 for databases
  • Version 7 - Modern time-based with improved sorting

For PHP applications:

  • Laravel models: Use Version 4 for Eloquent primary keys
  • Symfony entities: Consider Version 7 for time-ordered data
  • WordPress plugins: Use Version 5 for deterministic IDs

How do I generate UUID in other languages?

Web development:

  • JavaScript - crypto.randomUUID() & uuid library
  • TypeScript - Type-safe web development
  • Python - Built-in uuid module
  • Ruby - Rails and Sinatra applications

Enterprise development:

  • Java - java.util.UUID & UuidCreator
  • C# - System.Guid & UuidCreator

← Back to Online UUID Generator