Home  >  Article  >  Backend Development  >  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)

藏色散人
藏色散人Original
2019-03-15 15:22:002106browse

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";

Output:

生成的密码 : y4&#39;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!

Statement:
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