PHP getting random number experience _ PHP Tutorial

Source: Internet
Author: User
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

 
 
  1. < ?php
  2. class getRandstrClass
  3. {
  4. function getCode ($length = 32, $mode = 0)
  5. {
  6. switch ($mode) {
  7. case '1':
  8. $str = '1234567890';
  9. break;
  10. case '2':
  11. $str = 'abcdefghijklmnopqrstuvwxyz';
  12. break;
  13. case '3':
  14. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  15. break;
  16. case '4':
  17. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZa
    bcdefghijklmnopqrstuvwxyz';break;
  18. case '5':
  19. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXY
    Z1234567890';
  20. break;
  21. case '6':
  22. $str = 'abcdefghijklmnopqrstuvwxyz
    1234567890';
  23. break;
  24. default:
  25. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZa
    bcdefghijklmnopqrstuvwxyz1234567890';
  26. break;
  27. }
  28. $randString = '';
  29. $len = strlen($str)-1;
  30. for($i = 0;$i < $length;$i ++){
  31. $num = mt_rand(0, $len);
  32. $randString .= $str[$num];
  33. }
  34. return $randString ;
  35. }
  36. }
  37. ?>

Instructions for using PHP to obtain random numbers:

1. Save the above code as random. php
2. introduce random. php on the required page

 
 
  1. < ?php
  2. require_once('random.php');
  3. ?>

3. use the previously defined class

 
 
  1. < ?php
  2. $code = new getRandstrClass();
  3. $length = 32;
  4. $mode = 0;
  5. $str = $code->getCode($length, $mode);
  6. echo $str;
  7. ?>

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.