Skip to content

Instantly share code, notes, and snippets.

@abdullahbutt
Created September 30, 2018 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullahbutt/f79466d593685a5f0dc66355016b15fc to your computer and use it in GitHub Desktop.
Save abdullahbutt/f79466d593685a5f0dc66355016b15fc to your computer and use it in GitHub Desktop.
php generate random string up to x length
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
//Output the random string with the call below:
// Echo the random string.
// Optionally, you can give it a desired string length.
echo generateRandomString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment