- substr: documentation ( source)
- microtime: documentation ( source)
- mt_rand: documentation ( source)
- print_r: documentation ( source)
- array_unique: documentation ( source)
- sprintf: documentation ( source)
<?php
function uuid($c = 4) {
$x = function($c) {
for ($n = ''; strlen($n) < $c + 1; $n = $n.mt_rand());
return substr($n, -$c);
};
return sprintf(
'%s-%04d-%04d-%04d-%04d%04d',
microtime(true) * 1000, $x($c), $x($c), $x($c), $x($c), $x($c), $x($c)
);
}
$time = microtime();
$uuids = array();
for ($i = 0; $i < 10000; $i++) {
$uuids[] = uuid();
}
if (count(array_unique($uuids)) != count($uuids)) {
echo 'DUPLICATE';
} else {
echo 'No duplicate';
print_r($uuids);
}