All TalkersCode Topics

Follow TalkersCode On Social Media

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

PHP Random 5 Digit Number

Last Updated : Jul 1, 2023

PHP Random 5 Digit Number

In this tutorial we will show you the solution of PHP random 5 digit number, 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 5 digit number.

So, let us understand this.

Step By Step Guide On PHP Random 5 Digit Number :-

As, to generate and get a random number. Mainly we are using one function that is rand() function.

In this article, we are going to use and understand rand() and mt_rand() function. We will understand both of them later in detail in this article.

<!DOCTYPE html>
      <html>
        <head>
          <title> php random 5 digit number </title>
   </head>
 <body>
<?php
// by using rand function
echo(rand(10000,99999) . "<br>");
// by using mt_rand function
 echo mt_rand(10000,99999);
}
?>
 </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 create basic php structure in which <?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 10000 is minimum number where 99999 is maximum number.
  7. We know that we want 5 digit number. So, for minimum we give value of minimum 5 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. We hope that you understand the above codes properly with help of steps given here.
  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 get a random 5 digit number using php.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪