Home > Backend Development > PHP Tutorial > How to use shuffle() function to generate random password in PHP? (code example)

How to use shuffle() function to generate random password in PHP? (code example)

藏色散人
Release: 2023-04-05 16:08:01
Original
2133 people have browsed it

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.

How to use shuffle() function to generate random password in PHP? (code example)

# 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 = &#39;&#39;;

    //创建密码的内容
    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";
Copy after login

Output:

生成的密码 : y4&#39;8Z-by2sx
Copy after login

Function introduction:

chr( )Function returns the specified character

chr ( int $ascii ) : string
Copy after login

Returns a single character corresponding to ascii specified.

shuffle()Function to shuffle the array

shuffle ( array &$array ) : bool
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template