PHP function uniqid()

11. 09. 2019

Obsah článku

Availability in `PHP 4.0`

Generates a unique identifier.

It is mainly used for generating IDs in tables that are stored piecemeal on multiple servers and are periodically synchronized. If the database is running in multiple places at the same time, the uniqueness of each numeric ID cannot be guaranteed as an autoincrement (because there would have to be a central server that allocates them), so a UID is used, a string that has a different prefix on each machine and only its internal structure is generated, but the base remains the same. Thus, there is no collision of identifiers during subsequent synchronization.

The generated identifier contains hyphens and other characters. If we need to guarantee the same length of consecutive letters and digits, we can hash the identifier: md5(uniqid()).

Parameters

Parameter Data type Default value Note
$prefix string "" May be useful, for example, if you are generating identifiers simultaneously on multiple servers that might generate an identifier in the same microsecond.
$more_entropy bool false When set to true, uniqid adds additional entropy at the end of the return value (using a combined linear congruence generator), which should make the results more unique.

Return values

string

Unique identifier as a string.

Other resources

Official uniqid documentation

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články

1.

V jiných jazycích

2.