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 散列函数。生成的随机字符串将打印到控制台。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP