How to Generate 4,6,8,10 Digit Random number in PHP?
This tutorial is focused on how to generate 6 digit random number in php. This article will give you simple example of generate 4 digit unique number in php. it's simple example of generate 10 digit random number in php. This post will give you simple example of php random 5 digit number.
in this post, i will give you simple example of how to generate random digit using rand() and mt_rand() function in php. let's see bellow example one by one.
rand() Example:
<?php
/* php rand function */
$rand = rand();
echo($rand); /* Output: 2051787537*/
/* generate 2 digit unique random number in php */
$twodigitrandom = rand(10,99);
echo($twodigitrandom); /* Output: 63*/
/* generate 4 digit unique random number in php */
$fourdigitrandom = rand(1000,9999);
echo($fourdigitrandom); /* Output: 5222*/
/* generate 6 digit unique random number in php */
$sixdigitrandom = rand(100000,999999);
echo($sixdigitrandom); /* Output: 522285*/
/* generate 8 digit unique random number in php */
$eightdigitrandom = rand(10000000,99999999);
echo($eightdigitrandom); /* Output: 95522285*/
?>
mt_rand() Example:
<?php
/* php mt_rand function */
$rand = mt_rand();
echo($rand); /* Output: 2051787537*/
/* generate 2 digit unique random number in php */
$twodigitrandom = mt_rand(10,99);
echo($twodigitrandom); /* Output: 63*/
/* generate 4 digit unique random number in php */
$fourdigitrandom = mt_rand(1000,9999);
echo($fourdigitrandom); /* Output: 5222*/
/* generate 6 digit unique random number in php */
$sixdigitrandom = mt_rand(100000,999999);
echo($sixdigitrandom); /* Output: 522285*/
/* generate 8 digit unique random number in php */
$eightdigitrandom = mt_rand(10000000,99999999);
echo($eightdigitrandom); /* Output: 95522285*/
?>
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Connect SSH using PHP?
- Laravel PHP json_decode without quotes Example
- PHP Get All Array Keys Starting with Certain String Example
- How to Convert XML File to Array in PHP?
- PHP Copy File from One Folder to Another Example
- PHP Ajax Infinite Scroll Pagination Example
- PHP Import Excel File into MySQL Database Tutorial
- File Upload with Progress Bar using jQuery Ajax and PHP Example
- How to Convert Array Key to Lowercase in PHP?
- How to Find Day Name from Specific Date in PHP?
- How to Merge Two Array with Same Keys without Loop in PHP?
- How to use Google Maps API with JQuery PHP?