1 2 3 4 5 6 7 8 9 |
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:
1 2 3 |
// Echo the random string. // Optionally, you can give it a desired string length. echo generateRandomString(); |