PHP: Generating Multiple Random String

Here is a quick way to generate random strings in PHP. I will be showing how you can generate multiple random strings at once. You can specify the number of letters in the random string generated. You can also specify the total number of words you want to generate.

I have used str_shuffle function to shuffle the string and then substr function is used to fetch a portion of the string.

Here is the code:-


// string from which random strings are generated
$string = 'abcdefghijklmnopqrstuvwxyz';

// result array of random strings
$result = array();

// number of letters in the random string to be generated
$letters = 3;

// total number of word you want to generate
$num = 10;

for ($i=0;$i<$num;$i++) {
	$result[] = substr(str_shuffle($string), 0, $letters);
}

echo "<pre>"; print_r($result); echo "

“;

Hope this helps. Thanks.