To answer this question specifically, two problems:
$randstring
is not in scope when you echo it.Here's a code snippet with the corrections:
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
Output the random string with the call below:
// Echo the random string.
echo generateRandomString();
// Optionally, you can give it a desired string length.
echo generateRandomString(64);
I'm getting an IndentationError (or a TabError). How do I fix it?
How do I clone a list so that it doesn't change unexpectedly after assignment?
List of lists changes reflected across sublists unexpectedly
How to test multiple variables for equality against a single value?
How do I create variable variables?
Asking the user for input until they give a valid response
How can I access and process nested objects, arrays, or JSON?