PHP | uniqid( ) Function

The uniqid() function in PHP is an inbuilt function which is used to generate a unique ID based on the current time in microseconds (micro time).
The ID generated from the uniqid() function is not optimal since it is based on the system time and is not cryptographically secured. Thus it should not be for cryptographical purposes.

The uniqid( ) function accepts prefix and more_entropy as parameters and returns timestamp based unique identifier as a string.

Syntax:

 uniqid($prefix, $more_entropy) 

Parameters Used: The uiqid() function in PHP accepts two parameters.

  1. $prefix : It is an optional parameter which specifies a prefix to the unique id.
  2. $more_entropy : It is an optional parameter which specifies more entropy at the end of the return value which makes the id more unique.The default value is FALSE, which returns 13 characters long string whereas when it is set to TRUE, the return string is 23 characters long.

Return Value: It returns timestamp based unique identifier as a string.

Errors And Exceptions

  1. The uniqid() function tries to create unique identifier, but it does not guarantee 100% uniqueness of return value.
  2. 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.

Below programs illustrate the uniqid() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// generating unique id
echo uniqid(); 
?>

chevron_right


Output:

3b2c662647f18

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// generating unique id with pefix gfg
$myuid = uniqid(gfg);
  
echo $myuid;
?>

chevron_right


Output:

gfg5b2b451823970

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// generating unique id with pefix gfg 
// and higher entropy
$myuid = uniqid(gfg, true);
  
echo $myuid;
?>

chevron_right


Output:

gfg5b2b4555ab6bd7.27884925

Reference : http://php.net/manual/en/function.uniqid.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.