Open In App

PHP | random_bytes () Function

The random_bytes()is an inbuilt function in PHP. The main function is to generate cryptographically secure pseudo-random bytes. It generates cryptographic random bytes of arbitrary string length. The different sources of randomness used in this function, they are as follows:-

Syntax :



String random_bytes ( int $length )

Parameter : It is the length of random string, returned in bytes. Return Value: The function returns the cryptographically secure random bytes in string. Examples:

Input : length = 7
Output :string(14) "cbd392c01352b0"

Below programs illustrate the random_bytes () function in PHP. Program 1: 






<?php
//random_bytes () function in PHP
$length = random_bytes('4');
 
//Print the result and convert by binaryhexa
var_dump(bin2hex($length));
?>

Output:
string(8) "e62a94a2"




<?php
//random_bytes () function in PHP
$length = random_bytes('6');
 
//Print the result and convert by binaryhexa
var_dump(bin2hex($length));
?>

Output:
string(12) "808fc44d325b"

Exception Error :

References: http://php.net/manual/en/function.random-bytes.php

Article Tags :