Tag Archives: php random string function

Create a random string with PHP


How to create a random string with PHP.

Step 1:

Include the following function somewhere.

function randomstring($length)
  $string = "";
  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  return $string;

I’ve only included upper and lower case characters here, but you can add anything to the $chars variable, and they will be included and picked at random.


Just send the length of the string you want to return to the function when you call it, and it will return your random string.  So a random 15 character string would be:

$randomString= randomstring(15);


Share this post:
read more