PHP 程序生成随机字符串的不同方法


使用 bin2hex 函数

示例

 在线演示

<?php
$num = 12;
$res = bin2hex(random_bytes($num));
print_r("The randomly generated string is : ");
echo $res;
?>

输出

The randomly generated string is : f1db16115fa93b98493d388b

定义了一个数字并在该数字上调用了 bin2hex 函数。在 bin2hex 函数内,在此数字上调用了 `random_bytes` 函数。生成的随机字符串将打印到控制台。

使用散列函数

示例

 在线演示

<?php
$my_str = rand();
$res = sha1($my_str);
print_r("The randomly generated string using hashing function sha1 is :");
echo $res;
?>

输出

The randomly generated string using hashing function sha1 is :9a4a73c35ac034832332977f3d5accd8eace5260

通过调用 `rand` 函数来定义一个数字。在此随机生成数字上调用 sha1 散列函数。生成的随机字符串将打印到控制台。

使用内置函数 uniqid

示例

 在线演示

<?php
$res = uniqid();
print_r("The randomly generated string using uniqid function is : ");
echo $res;
?>

输出

The randomly generated string using uniqid function is : 5ed4b884cef34

通过调用 `rand` 函数来定义一个数字。在此随机生成数字上调用 sha1 散列函数。生成的随机字符串将打印到控制台。

更新日期:02-07-2020

264 次查看

启动您的事业

完成课程即可获得认证

开始学习
广告
© . All rights reserved.