PHP random string generator function


/**
* @param $length
* @return string
* @throws Exception
*/
function generateRandomString($length) {
$bytes = random_bytes($length);
return bin2hex($bytes);
}


  • Generate Random Bytes:
    • Utilizes the random_bytes function to generate a string of random bytes with a length specified by the input parameter.
  • Convert to Hexadecimal:
    • Converts the generated random bytes to a hexadecimal representation using the bin2hex function.
  • Return Random String:
    • Returns the resulting hexadecimal string, which serves as a secure and random identifier or token.

Last updated

Comments

Leave a Comment