79238583

Date: 2024-11-29 23:48:45
Score: 1
Natty:
Report link

I'd use a local variable and a WHILE loop to avoid the recursive function call:

public function generateToken($length = 5)
{
    $token_exists = true;
    while ($token_exists) {
        $token = strtoupper(substr(md5(rand()), 0, $length));
        $token_exists = $this->tokenExistsAlready();
    }
    return $token;
}
Reasons:
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Tom