← All built-ins / random_bytes()

random_bytes()

random_bytes(int $n): string

Parameters

NameTypeDescription
$nintbyte count (1–1024)

Return value

$n cryptographically secure random bytes as a binary string.

Example

$tok = bin2hex(random_bytes(16));
echo strlen($tok) == 32 ? "token-ok" : "token-bad";
token-ok

See also

random_int()bin2hex()password_hash()