PHP experiences getting random numbers. Purpose: obtain random string parameters for PHP to obtain random numbers: 1, (int) $ length32 # random character length 2, (int) $ mode0 # random character type, 0 is a combination of uppercase and lowercase English letters and numbers.
Purpose:
Returns a random string.
Parameters for PHP to obtain a random number:
1. (int) $ length = 32 # random character length
2. (int) $ mode = 0 # random character type. 0 indicates uppercase and lowercase English letters and numbers, 1 indicates numbers, 2 indicates lowercase letters, 3 indicates uppercase letters, and 4 indicates uppercase and lowercase letters, 5 is an uppercase letter and number, and 6 is a lowercase letter and number.
PHP code for getting a random number definition
- < ?php
- class getRandstrClass
- {
- function getCode ($length = 32, $mode = 0)
- {
- switch ($mode) {
- case '1':
- $str = '1234567890';
- break;
- case '2':
- $str = 'abcdefghijklmnopqrstuvwxyz';
- break;
- case '3':
- $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- break;
- case '4':
- $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZa
bcdefghijklmnopqrstuvwxyz';break;
- case '5':
- $str = 'ABCDEFGHIJKLMNOPQRSTUVWXY
Z1234567890';
- break;
- case '6':
- $str = 'abcdefghijklmnopqrstuvwxyz
1234567890';
- break;
- default:
- $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZa
bcdefghijklmnopqrstuvwxyz1234567890';
- break;
- }
- $randString = '';
- $len = strlen($str)-1;
- for($i = 0;$i < $length;$i ++){
- $num = mt_rand(0, $len);
- $randString .= $str[$num];
- }
- return $randString ;
- }
- }
- ?>
Instructions for using PHP to obtain random numbers:
1. Save the above code as random. php
2. introduce random. php on the required page
- < ?php
- require_once('random.php');
- ?>
3. use the previously defined class
- < ?php
- $code = new getRandstrClass();
- $length = 32;
- $mode = 0;
- $str = $code->getCode($length, $mode);
- echo $str;
- ?>
The above is all the application methods for PHP to obtain random numbers.
Parameters used to obtain a random number from a random string in PHP: 1, (int) $ length = 32 # random character length 2, (int) $ mode = 0 # random character type, 0 is a combination of uppercase and lowercase English letters and numbers...