Easy Tutorial
❮ Pdo Getattribute Func Mysqli Affected Rows ❯

PHP CSPRNG

PHP 7 New Features

CSPRNG (Cryptographically Secure Pseudo-Random Number Generator).

PHP 7 introduces several CSPRNG functions to provide a simple mechanism for generating cryptographically secure random numbers.

-

random_bytes() - Generates a random string.

-

random_int() - Generates a random integer.


random_bytes()

Syntax

string random_bytes ( int $length )

Parameters

-

length - The number of bytes to return in the random string.

Return Value

-

Returns a string, accepting an int parameter representing the number of bytes in the result.

Example

<?php
$bytes = random_bytes(5);
print(bin2hex($bytes));
?>

Output of the above program:

6f36d48a29

random_int()

Syntax

int random_int ( int $min , int $max )

Parameters

-

min - The minimum value to return, must be greater than or equal to PHP_INT_MIN.

-

max - The maximum value to return, must be less than or equal to PHP_INT_MAX.

Return Value

-

Returns an integer within the specified range.

Example

<?php
print(random_int(100, 999));
print(PHP_EOL);
print(random_int(-1000, 0));
?>

Output of the above program:

723
-64

PHP 7 New Features

❮ Pdo Getattribute Func Mysqli Affected Rows ❯