Generating a random numbers and letters

前端 未结 15 1087
梦如初夏
梦如初夏 2020-12-25 08:23

Can I ask for a certain code on how to generate a random letters and numbers in one word. I know there\'s a PHP function rand(), but I don\'t know if it\'s also applicable w

15条回答
  •  有刺的猬
    2020-12-25 08:42

    // A string with random letters and numbers. A-Z, a-z, 0-9
    
    
    // A function in PHP is a block of code that can be used elsewhere in code.
    // This function is called rand_string and will generate a random sequence of characters in one string.
    // A default value of 16 characters is set, so that if no integer is supplied it will use the value of 16.
    function rand_string($length = 16) {
    
        // A string is something that holds alphanumeric characters and other symbols. 
        // This string is an empty one, or at least that's how it starts.
        $string = '';
    
        // This is known as a for/next loop, it's will run a section of code for a set number of times.
        // A counter $i is incremented on each pass. In this case until it has operated $length number of times.
        for ($i = 0; $i < $length; $i++) {
    
            // This variable ($die) is assigned a random number - which is obtained via the PHP function mt_rand.
            //Consult the PHP docs for more information.
            $die = mt_rand(1, 3);
    
            // This switch statement picks a case that is true and runs the accompanying code as defined in each case.
            switch ($die) {
    
                // This case will be activated if the variable $die has the value of 1. And case 2 if it has the value of 2 and so on.
                case 1:
                    // Here and subsequently a random value between 48 and 57 is assigned to the $rnd variable.
                    $rnd = mt_rand(48, 57);
                    break;
    
                case 2:
                    $rnd = mt_rand(65, 90);
                    break;
    
                case 3:
                    $rnd = mt_rand(97, 122);
                    break;
            }
    
            // This is another variable $string which is assigned the ASCII character that is represented by the $rnd variable.
            // ASCII characters are codes that computers use to represent characters and symbols.
            // The chr function is a special PHP function that returns the character represented by the ASCII code.
            // In this case the value of $rnd.
            $string .= chr($rnd);
        }
    
        // Here we reach the final result. The value of $string is returned to source of the function call.
        return $string;
    }
    
    // Segments of the function, loops and switches are enclosed between curly brackets {}. This limits the scope of the processing contained within.
    
    // Usage of this function to obtain a 10 character random string.
    // echo is a function that prints the result to the browser/screen.
    $mystring = rand_string(10);
    echo $mystring;
    

提交回复
热议问题