Random\Randomizer::getBytes
(PHP 8 >= 8.2.0)
Get random bytes
Description
Generates a string containing uniformly selected random bytes with the requested length.
As the returned bytes are selected completely randomly, the resulting string is likely to contain unprintable characters or invalid UTF-8 sequences. It may be necessary to encode it before transmission or display.
Parameters
lengthThe length of the random String that should be returned in bytes; must be
1or greater.
Return Values
A String containing the requested number of random bytes.
Errors/Exceptions
- If the value of
lengthis less than1, aValueErrorwill be thrown.
Examples
Random\Randomizer::getBytes() example
<?php
$r = new \Random\Randomizer();
echo bin2hex($r->getBytes(8)), "\n";
?>The above example will output something similar to:
ebdbe93cd56682c2See Also
random_bytes()bin2hex()base64_encode()Random\Randomizer::getBytesFromString