Generate 8 Digit Unique Number In PHP
Last Updated : Jan 1, 2023
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>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- 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.
- 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.
- 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.
- We know that we want 8 digit number. So, for minimum we give value of minimum 8 digit number and same for maximum.
- 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().
- 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.
- At last, the <body> and <html> tags are closed with </body> and </html> respectively.
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.