导航首页 » 技术教程 » PHP:uniqid()的用法_misc函数
PHP:uniqid()的用法_misc函数 142 2023-12-15   

uniqid

(PHP 4, PHP 5)

uniqid — 生成一个唯一ID

说明

string uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] )

获取一个带前缀、基于当前时间微秒数的唯一ID。

参数

prefix

有用的参数。例如:如果在多台主机上可能在同一微秒生成唯一ID。

prefix为空,则返回的字符串长度为13。more_entropyTRUE,则返回的字符串长度为23。

more_entropy

如果设置为 TRUEuniqid() 会在返回的字符串结尾增加额外的煽(使用combined linear congruential generator)。 使得唯一ID更具唯一性。

返回值

返回字符串形式的唯一ID。

范例

Example #1 uniqid() 例子

<?php
/* A uniqid, like: 4b3403665fea6 */
printf("uniqid(): %srn", uniqid());

/* We can also prefix the uniqid, this the same as 
 * doing:
 *
 * $uniqid = $prefix . uniqid();
 * $uniqid = uniqid($prefix);
 */
printf("uniqid('php_'): %srn", uniqid('php_'));

/* We can also activate the more_entropy parameter, which is 
 * required on some systems, like Cygwin. This makes uniqid()
 * produce a value like: 4b340550242239.64159797
 */
printf("uniqid('', true): %srn", uniqid('', true));
?>

更新日志

版本 说明 5.0.0 prefix 参数设为可选。 4.3.1 prefix 参数的长度限制提升到114个字符。

注释

Note:

在Cygwin环境下,为了使此函数能够工作,more_entropy 必须设置为 TRUE


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103