Avatar of MMDeveloper
MMDeveloperFlag for United States of America

asked on 

uniqid isn't soo unique?

So I was driving home from work the other day and had a thought come to mind.. In PHP, the uniqid function is based on the microtime. If you are calling the uniqid function in a loop on a relatively fast server (that can run more than 1000 calculations per second (which is not difficult these days), wouldn't uniqid return identical results for 1/1000th of a second? Granted the number of identical results returned in that 1/1000th of a second would depend on the speed of the server.


I have not put this to a test, was just a theory/thought I had. Any thoughts?
$i = 0;
$output = array ();
 
while ($i < 1000) {
      $output[] = uniqid();
      ++$i;
}
 
print_r($output);

Open in new window

PHP

Avatar of undefined
Last Comment
psimation
ASKER CERTIFIED SOLUTION
Avatar of brad2575
brad2575
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of psimation
psimation
Flag of South Africa image

<?php
error_reporting(E_ALL | E_NOTICE);
echo "starting:
";
$i = 0;
$duplicate = 0;
$output = array ();
 
while ($i < 1000) {
      $output[] = uniqid();
      ++$i;
      if (in_array($output[$i],$output)){
      $duplicate++;
      }
}
 
echo "duplicates: ".$duplicate;
?>

This doesn't give me any duplicates?
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo