All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Generate 6 Digit Unique Random Number In PHP

Last Updated : Jul 1, 2023

Generate 6 Digit Unique Random Number In PHP

In this tutorial we will show you the solution of generate 6 digit unique random number in PHP, mainly there are many methods with help of which we can generate random number.

But the main question here is that the random number should be 6 digit number and it must be unique.

Step By Step Guide On Generate 6 Digit Unique Random Number In PHP :-

As, to generate and get a random number. We are going to use here different methods.

First one is with help of rand(), next one is by using mt_rand() and last one is by random_int() method.

Let us understand them with help of codes given below.

<!DOCTYPE html>
      <html>
        <head>
          <title> generate 6 digit unique random number in php </title>
   </head>
 <body>
<?php
// by using rand function
echo(rand(100000,999999) . "<br>");
// by using mt_rand function
 echo mt_rand(100000,999999);
// by using random_int function
 echo random_int(100000,999999);
?>
 </body>
      </html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as you see all the methods are same. First of all we use rand, then we use mt_rand and last one is by using random_int.
  6. In all we see that we use 100000 and 999999. This is because in all of them there must be two parameters and these two are minimum number and maximum number. And we know that there must be a limit or say range of length of every number.
  7. For a six digit number, it smallest number is 100000 whereas its maximum number is 999999. When we go below 100000 this number, the number is considered as 5 digit number. And when we go after 999999 then the number is known as 7 digit number.
  8. So, using rand we give limits to rand function and it show always a unique six digit number and a random number also. We hope that you understand the codes easily with help of these steps of explanation.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to get a random 6 digit unique number using php.

I hope this tutorial on generate 6 digit unique random number in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪