Skip to content

Instantly share code, notes, and snippets.

@thelebster
Forked from james2doyle/uniqid.php
Created May 6, 2017 10:23
Show Gist options
  • Select an option

  • Save thelebster/64d852499e29e4c70e3277c48cb3f375 to your computer and use it in GitHub Desktop.

Select an option

Save thelebster/64d852499e29e4c70e3277c48cb3f375 to your computer and use it in GitHub Desktop.
PHP uniqid function written in pure PHP
<?php
/*
* Use this if you need to recreate `uniqid` in another language
* From: http://php.net/manual/en/function.uniqid.php#95001
*/
// time in microseconds as float
$m = microtime(true);
// show me the results
echo sprintf("%8x%05x\n", floor($m), ($m - floor($m)) * 1000000);
// show me the real call to `uniqid`
echo uniqid();
@danomatic

Copy link
Copy Markdown

Note, the newline is not part of the uniqid function:

echo sprintf("%8x%05x", floor($m), ($m - floor($m)) * 1000000) . "\n";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment