All TalkersCode Topics

Follow TalkersCode On Social Media

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

Generate 8 Digit Unique Number In PHP

Last Updated : Mar 11, 2024

Generate 8 Digit Unique Number In PHP

In this tutorial we will show you the solution of generate 8 digit unique number in PHP, as we know random numbers are useful when we want to generate a sequence of random events.

And there are many formats to print random number like 2 digit, 5 digit, etc. whereas in this article we will understand about 8 digit random number.

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

As, there are many ways to generate an 8 digit random number.

The best two ways from our self are by using rand() and mt_rand() function. There main not any big difference between them.

Let us understand first them with help of codes.

<!DOCTYPE html>
      <html>
        <head>
          <title> php tutorials </title>
   </head>
 <body>
<?php
// by using rand function
echo(rand(10000000,99999999) . "<br>");
// by using mt_rand function
 echo mt_rand(10000000,99999999);
}
?>
 </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, inside body we use our php code. For php code, we use our starting tag and also use ending tag. <?php is our starting tag for php whereas ?> is ending tag for php.
  6. Inside these tags we use our rand function. rand function is used to generate a random number and it has two parameters first one is min and another is max. min is the minimum number as max is maximum number. Here we give 10000000 is minimum number where 99999999 is maximum number.
  7. We know that we want 8 digit number. So, for minimum we give value of minimum 8 digit number and same for maximum.
  8. Whereas when we see mt_rand(), we found that it is same like rand. But there is one difference of syntax that is mt_rand or rand().
  9. But there is one main difference between them that is mt_rand is successor of rand function. mt_rand is four time faster than rand function. Beside this there is no more difference.
  10. 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 generate 8 digit unique number in php.

I hope this tutorial on generate 8 digit unique 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 🡪