I'm getting an Undefined variable: key for this
$key .= $keys[mt_rand(0, count($keys) - 1)];
I'm getting an Undefined variable: key for this
$key .= $keys[mt_rand(0, count($keys) - 1)];
The . of the .= is trying to append to a previously existing $key, which you must not have (for example first iteration of a loop). Initialize $key as NULL or empty at the start of your script.
$key = '';