Home >Backend Development >PHP Tutorial >How to use shuffle() function to generate random password in PHP? (code example)
In this article, we will introduce to you how to use the PHP shuffle() function to generate a random password, whose password contains uppercase, lowercase, numbers and others.
# Below we will introduce the method of generating random passwords by the shuffle() function based on specific code examples.
PHP code example is as follows:
<?php function rand_Pass($upper = 1, $lower = 5, $numeric = 3, $other = 2) { $pass_order = Array(); $passWord = ''; //创建密码的内容 for ($i = 0; $i < $upper; $i++) { $pass_order[] = chr(rand(65, 90)); } for ($i = 0; $i < $lower; $i++) { $pass_order[] = chr(rand(97, 122)); } for ($i = 0; $i < $numeric; $i++) { $pass_order[] = chr(rand(48, 57)); } for ($i = 0; $i < $other; $i++) { $pass_order[] = chr(rand(33, 47)); } //使用shuffle()来打乱顺序 shuffle($pass_order); //最终密码字符串 foreach ($pass_order as $char) { $passWord .= $char; } return $passWord; } echo "\n"."生成的密码 : ".rand_Pass()."\n";
Output:
生成的密码 : y4'8Z-by2sx
Function introduction:
chr( )Function returns the specified character
chr ( int $ascii ) : string
Returns a single character corresponding to ascii specified.
shuffle()Function to shuffle the array
shuffle ( array &$array ) : bool
This function shuffles (randomly arranges the order of cells) an array. It uses a pseudo-random number generator and is not suitable for cryptography situations.
This article is about using the shuffle() function to generate random passwords. I hope it will be helpful to friends in need!
The above is the detailed content of How to use shuffle() function to generate random password in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!