Generate PHP Random String

It’s easy to use to use the rand(min, max) function to generate a random number between min and max (inclusive).

Here is a clever way of using rand() and chr() function to generate a random string:

chr(rand(65,90)) will generate a random character between A – Z (capital)

chr(rand(97,122)) will generate a random character between a – z (lower case)

Therefore, if you want to generate a 5 character long string of random string (assuming all lower case), you would write:

$RandomString=chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122));

Or you can write a simple for() loop and concatenate them together.

Hope that helps.

Comments are closed.