All TalkersCode Topics

Follow TalkersCode On Social Media

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

Generate 4 Digit Random Number In PHP

Last Updated : Mar 11, 2024

Generate 4 Digit Random Number In PHP

In this tutorial we will show you the solution of generate 4 digit random number in PHP, mainly there are many methods with help of which we can generate random number, but our task is to generate a random number of length 4.

So, let us see how to generate

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

As, first of all there are many methods with help of which we are able to generate a random number of 4 digits in php.

In this article, we are going to use two main function or say methods for this. These are rand() and mt_rand(). Let us see how to generate a 4 digit number from them and difference between them.

<!DOCTYPE html>
      <html>
        <head>
          <title> generate 4 digit random number in php </title>
   </head>
 <body>
<?php
// by using rand function
echo(rand(1000,9999) . "<br>");
// by using mt_rand function
 echo mt_rand(1000,9999);
}
?>
 </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 1000 is minimum number where 9999 is maximum number.
  7. We know that we want 4 digit number. So, for minimum we give value of minimum 4 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 generate 4 digit random number in php.

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