PHP Function Reference

PHP uniqid() Function



The PHP uniqid() function is used to generate a prefixed unique identifier based on the current time in microseconds.

Note: This function does not guarantee uniqueness of return value. Since most systems adjust system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return unique ID for the process/thread. The more_entropy parameter is used to increase likelihood of uniqueness.

Syntax

uniqid(prefix, more_entropy)

Parameters

prefix Optional. Specify a prefix to the unique ID. It can be useful, for instance, if you generate identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond.
more_entropy Optional. If set to true, this function will add additional entropy at the end of the return value, which increases the likelihood that the result will be unique. With an empty prefix, the returned string will be 13 characters long. If more_entropy is true, it will be 23 characters long.

Return Value

Returns timestamp based unique identifier as a string.

Example: uniqid() example

The example below shows the usage of uniqid() function.

<?php
//using uniqid() function without any parameter
echo uniqid()."\n";

//using prefix parameter and the return value
//will be equivalent to prefix.uniqid()
echo uniqid("php_")."\n";

//using more_entropy parameter, this will add additional 
//entropy at the end of the return value and the return 
//value will be 23 characters long
echo uniqid("", true)."\n";

//using prefix and more_entropy parameters together
echo uniqid("php_", true)."\n";
?>

The output of the above code will be similar to:

6199df41a8dba
php_6199df41a8dd7
6199df41a8dde5.55584947
php_6199df41a8dea3.57983292

❮ PHP Miscellaneous Reference