Random string generator

random string generator in PHP

Random string are often used in web application to assign records a unique random key. Here is very useful random string generator function that allows you generate random string of any length.

Just call the function and pass a length. If you omit length parameter it will generate a 10 characters long alphanumeric string.

function generate_string($length=10){
        $chars = array_merge(range(0, 9),  range('A', 'Z'), range('A', 'Z'));
        $string = implode(array_slice($chars, 0, $length));
        return $string;

Author: Tahir Yasin

Tahir is a Passionate Web Developer from Lahore, Pakistan and a guy with NEVER-STOP-LEARNING attitude who keeps an eye on emerging technologies and customer trends.

3 thoughts on “Random string generator”

  1. Hello Tahir,
    I think the third rang(‘A’, ‘Z’) should be rang(‘a’, ‘z’), otherwise, your generated string will contains no lower letters.

    1. Yes, you are right but I intentionally capitalized, but you can change their case if you need both upper and lower case letters.

Leave a Reply

Your email address will not be published. Required fields are marked *