为什么PHP的uniqid函数只返回13位数,而不是14?

前端之家收集整理的这篇文章主要介绍了为什么PHP的uniqid函数只返回13位数,而不是14?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
uniqid()函数返回13位数十六进制数.根据 spec in php.net site,该功能使用microtime生成唯一的值.

但是microtime会以字符串格式返回数字,如下所示:

"0.70352700 12689396875"

这些基本上是从1970年以来的微秒和秒数.
这是一个9 11位十进制数字.

将十进制数十进制转换为十六进制将导致16位十六进制,而不是13位数.

我也想出了“0”似乎永远不会改变的部分,微秒部分的最后两位数似乎总是“00”.这样做,十进制数将只有9 11-3个数字长,但是当转换成十六进制时仍然是十七进制数,将导致14位十六进制数字不是13.

我不会以另一种方式获得一个独特的身份证,或者长期/短期的独特的身份证号码!我只要知道如果有人知道为什么单向退货只有13个数字.

看起来好像是nosense:如果uniqid返回的数字少于microtime,这意味着microtime会发出更独特的结果,而uniqid返回的结果更加独特.

发现在 http://www.php.net/manual/en/function.uniqid.php#95001

对我有意义评论如果您需要解释

For the record,the underlying
function to uniqid() appears to be
roughly as follows:

$m=microtime(true);
sprintf(“%8x%05x\n”,floor($m),($m-floor($m))*1000000);

In other words,first 8 hex chars =
Unixtime,last 5 hex chars =
microseconds. This is why it has
microsecond precision. Also,it
provides a means by which to
reverse-engineer the time when a
uniqid was generated:

date(“r”,hexdec(substr(uniqid(),8)));

Increasingly as you go further down the string,the number becomes “more unique” over time,with the exception of digit 9,where numeral prevalence is 0..3>4>5..f,because of the difference between 10^6 and 16^5 (this is presumably true for the remaining digits as well but much less noticeable).

猜你在找的PHP相关文章