Generating a random string.

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42154707/article/details/94574565

1. The most stupid way: write a method call, as follows: 

function getRandom($number){
        $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//想要随机的字符
        $key = "";//生成随机数的容器
        for($i=0;$i<$number;$i++)
        {
            $key .= $str{mt_rand(0,32)};    //生成php随机数
        }
        return $key;
    }

Direct method calls, parameters which fill much, how much is generated bits, such as filling 32, is 32, and 16 is 16 bits.

2. If it is yii2 frame, you can call comes direct method:

Yii::$app->security->generateRandomString();

generateRandomString (); without parameters, the default is 32-bit random number, other desired number of bits, can directly enter the number.

3. Direct call:

Uuid::uuid();

 

Guess you like

Origin blog.csdn.net/qq_42154707/article/details/94574565