A function of generating random numbers in PHP

Source: Internet
Author: User
    1. function Randomkeys ($length)
    2. {
    3. $pattern = ' 1234567890abcdefghijklmnopqrstuvwxyz
    4. Abcdefghijklomnopqrstuvwxyz,./&l
    5. t;>?; #:@~[]{}-_=+) (*&^%___fckpd___0pound; "!"; Character descriptors
    6. for ($i =0; $i < $length; $i + +)
    7. {
    8. $key. = $pattern {Mt_rand (0,35)}; Generate PHP Random number
    9. }
    10. return $key;
    11. }
    12. Echo Randomkeys (8); This PHP random function can generate xc*=z~7l such a string, enough random! Here's another way to generate random numbers with PHP: Use the Chr () function to eliminate the steps of creating a descriptors. function Randomkeys ($length)
    13. {
    14. $output = ";
    15. for ($a = 0; $a < $length; $a + +) {
    16. $output. = Chr (Mt_rand (33, 126)); Generate PHP Random number
    17. }
    18. return $output;
    19. }
    20. Echo Randomkeys (8);
    21. ?>
Copy CodeNote: In the second PHP random function, use Mt_rand () to generate a PHP random number between 33 and 126, and then use the Chr () function to convert to characters. Looking at the ASCII code table, you will find that 33 to 126 represents all the characters of the character Fu Chili in the first function. The second function is functionally identical to the first function and is more concise.
  • 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.