Programster's Blog

Tutorials focusing on Linux, programming, and open-source

PHP Lecture - Identify All the Things With UUIDs!

After hearing about UUIDs from a talk on Doctrine ORM good practices, I wanted to learn a little bit more about them before starting to use them in my databases. Below is a lightning talk from the author/maintainer of ramsey/uuid, the PHP package library recommended in the Doctrine lecture for generating the UUIDs.

Timestamps

Conclusion

Hopefully you found that video helpful. The main points I took from it are that:

  • Versions 1,2, and 4 generate new unique UUIDs from random bytes, but versions 3 and 5 will always generate the same UUID based on the inputs given to them.
  • Versions 3 and 5 are the same, except 5 uses a different hashing algorithm, SHA-1 instead of md5
  • Version 4 can be made to be sequential, making it more suitable for databases so that later records are always appended, rather than injected randomly throughout the table.

Update - New UUID Versions

Since having made this tutorial, several more UUID standards have come out and supported by the Ramsey/uuid package. These includes version 6 which is pretty much a replacement for v1, and version 7, which is a mix of versions 1 and 4. Version 7 addresses the need for time-ordering for databases, but still provide the randomness in v4 for the need to be able to generate multiple UUIDs at the exact same moment in time, and not be able to "guess" the identifier for security reasons.

Appendix

Below is my tweaked version of the example code that generated sequentialy UUID v4.

<?php

/*
 * In order to be able to run this script, you need to use the following
 * commands in order to install the necessary packages:
 *  composer require ramsey/uuid
 *  composer require moontoast/math 
 */

require_once(__DIR__ . '/vendor/autoload.php');

$factory = new \Ramsey\Uuid\UuidFactory();

$generator = new \Ramsey\Uuid\Generator\CombGenerator(
    $factory->getRandomGenerator(), 
    $factory->getNumberConverter()
);

$codec = new Ramsey\Uuid\Codec\TimestampFirstCombCodec($factory->getUuidBuilder());

$factory->setRandomGenerator($generator);
$factory->setCodec($codec);

Ramsey\Uuid\Uuid::setFactory($factory);

for ($i=0; $i<5; $i++)
{
    echo Ramsey\Uuid\Uuid::uuid4()->toString() . PHP_EOL;
}

You generally no longer need to do this with the release of version 7, so the code above could now be replaced with:

<?php

/*
 * In order to be able to run this script, you need to use the following
 * commands in order to install the necessary packages:
 *  composer require ramsey/uuid
 *  composer require moontoast/math 
 */

require_once(__DIR__ . '/vendor/autoload.php');

for ($i=0; $i<5; $i++)
{
    echo Ramsey\Uuid\Uuid::uuid7()->toString() . PHP_EOL;
}

Not only that, but PostgreSQL 18 now has the uuidv7() function, to have PostgreSQL generate these UUIDs for you if you need.

Last updated: 25th August 2025
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch