Generate random string in PHP

Here’s a function to create a random string with the chars ‘a’ to ‘z’, ‘A’ to ‘Z’ and 0 to 9 in PHP.
I have seen many people asking for this, so here it is:

<?php

function randStr($length, $str='') {
	$array = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
    for($i = 0; $i < $length; $i++) {
        $str .= $array[rand(0, (count($array) - 1))];
    }
    return $str;
}

?>

Tags:

Leave a Reply