使用 PHP 生成随机字符串

phpserver side programmingprogramming更新于 2025/5/8 17:22:17

使用 PHP 生成随机字符串,代码如下−

示例

<?php
   $res = substr(md5(mt_rand()), 0,5);
   echo "Displaying random string...
";    echo $res; ?>

输出

这将产生以下输出−

Displaying random string...
1c856

示例

现在让我们看另一个例子 −

<?php
   $res = substr(md5(mt_rand()), 0,5);
   echo "Displaying random string...
";    echo $res;    echo "
Displaying another random string...
";    $res2 = bin2hex(random_bytes(5));    echo $res2; ?>

输出

这将产生以下输出−

Displaying random string...
a3541
Displaying another random string... 
335b83d9e9

相关文章