ItSolutionStuff.com

How to Generate 4,6,8,10 Digit Random number in PHP?

By Hardik Savani • May 14, 2024
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...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Connect SSH using PHP?

Read Now →

Laravel PHP json_decode without quotes Example

Read Now →

PHP Get All Array Keys Starting with Certain String Example

Read Now →

How to Convert XML File to Array in PHP?

Read Now →

PHP Copy File from One Folder to Another Example

Read Now →

PHP Ajax Infinite Scroll Pagination Example

Read Now →

PHP Import Excel File into MySQL Database Tutorial

Read Now →

File Upload with Progress Bar using jQuery Ajax and PHP Example

Read Now →

How to Convert Array Key to Lowercase in PHP?

Read Now →

How to Find Day Name from Specific Date in PHP?

Read Now →

How to Merge Two Array with Same Keys without Loop in PHP?

Read Now →

How to use Google Maps API with JQuery PHP?

Read Now →