random_int() returns a uniformly selected integer in the inclusive range you provide and is suitable for security-sensitive uses that need an unpredictable integer.
$code = random_int(100000, 999999);
echo $code;
Start with this current pattern. The examples below are retained for additional variations, output and reference.
echo random_int(8,10); // Random Output starting from 8 to 10
Syntax
random_int ( min,max);
min ,max : Minimum and maximum value ( both inclusive ) of random integer to be generated ( both to be given )
<?php
echo random_int(1, 100); // Output: A random number between 1 and 100
?>
<?php
echo random_int(1000, 9999); // Secure random number between 1000 and 9999
?>
<?php
for ($i = 0; $i < 6; $i++) {
echo random_int(1, 49) . " "; // Output: 6 random lottery numbers between 1 and 49
}
?>
<?php
echo random_int(-50, -1); // Output: A random negative number between -50 and -1
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.