Skip to content

Short unique id in php

In PHP, you can use the function uniqid() to generate a short unique ID. It generates a unique ID based on the current time in microseconds and can be prefixed with a string if desired. Note that uniqid() is time-based and not cryptographically secure. For better collision avoidance under high load, use the more_entropy parameter:

Example of generating a short unique ID in PHP

php
<?php

$short_id = uniqid('', true);

or

Another example of generating a short unique ID in PHP

php
<?php

$prefix = 'abc_';
$short_id = $prefix . uniqid('', true);

This will generate a unique ID that looks something like this: abc_5f8912b1c1b24

For a cross-platform alternative, bin2hex(random_bytes(8)) is recommended. Alternatively, com_create_guid() can generate a GUID, but it is Windows-only and requires the COM extension.

Example of using com_create_guid() to generate a unique ID

php
<?php

$guid = com_create_guid();

This will generate a unique ID which looks like this: {3B5F6F80-B248-4E81-9F38-095E6A5D4C6F}

Dual-run preview — compare with live Symfony routes.