How to Generate 6 Digit Unique Number Using PHP?

Today i will tell you how to generate random 6 digit number using php. i will guide step by step. let’s go

rand() function

Today we will understand through this article about the random function of php, so for this we have a function rand() inside it we pass 2 parameters min and max inside it we put 2 any value whenever we Refers the page. So any random value in between will pick up and give.

Syntax:-

<?php
rand(min,max);
?>

Example:

<?php
$randomnumber_sixdigits = rand(100000,999999);
echo $randomnumber_sixdigits;
?>

mt_rand() function

Another function comes as soon as we get this mt_rand() function, but it is 4 times faster than rand. In this also two parameters are passed. The full form of min and max mt is Mersenne twister algorithm

Syntax:

<?php
mt_rand(min,max);
?>

Example:

<?php
$randomnumber_sixdigits = mt_rand(100000,999999);
echo $randomnumber_sixdigits;
?>

You can generate a random number by creating a function in php and running a loop in this function.

Example with php create by function

<?php
function generateCode($limit){
$code = '';
for($i = 0; $i < $limit; $i++) { $code .= mt_rand(0, 9); }
return $code;
}
echo generateCode(6);
?>