- 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(
'%x-%04x-%04x-%04x-%04x%04x%04x',
microtime(true) * 10000, $x($c), $x($c), $x($c), $x($c), $x($c), $x($c)
);
}
$time = microtime();
$uuids = array();
for ($i = 0; $i < 1000; $i++) {
$uuids[] = uuid();
}
if (count(array_unique($uuids)) != count($uuids)) {
echo 'DUPLICATE';
} else {
echo 'No duplicate';
print_r($uuids);
}