Creating a random string in PHP

Here’s a very useful function to generate a random string in php. I use this function quite often especially while working in core php projects.

function create_random_string($length=9)	{
	$salt = "abTUcdeIMNfjkl2m345noRqJKLr0s7tuvw1xyzABCD68EFGHpOPQSVWghiXYZ9".time();
	srand((double)microtime()*1000000);
	$res='';
	$i=0;
	while( $i <= $length )
		{
	$num = rand()%33 ;
	$new = substr($salt,$num,1);
	$res = $res.$new;
	$i++;
		}
	return $res;
}

Leave a Reply