Generate 6,8,10 digit random, unique, alphanumeric string and Number in PHP

Generate 6,8,10 digit random, unique, alphanumeric string and Number in PHP

If you are searching for such as generate php random string fixed length, generate random string in php without duplication, php, generate unique 6 digit unique number in php alphanumeric string, how to generate a unique string in php , generate php random md5 string, php random password, and php random alphanumeric string etc. So this tutorial will help you a lot.

In this tutorial, we will show you how you can easily generate 6,8,10 digit random, unique, alphanumeric string and number in PHP.

When you are working with PHP. So many times you need to generate random, unique, alphanumeric type string in your PHP projects. This tutorial will simply help you.

you can also read this php posts:- Generate 4,6,8,10 digits Unique Random Number in PHP.

Generate 6,8,10 digit random, unique, alphanumeric string and Number in PHP

  • Method 1:- Using str_shuffle() Function
  • Method 2:- Using md5() Function
  • Method 3:- To generate random, unique, alphanumeric numbers in PHP
    • 6-digit alphanumeric number
    • 8-digit alphanumeric number

Method 1:- Using str_shuffle() Function

The PHP str_shuffle() function is an built-in function in PHP. Basically which is used to randomly shuffle all the characters of a string passed to the function as a parameter. When a number is passed, it treats the number as the string and shuffles it. This function does not make any change in the original string or the number passed to it as a parameter. Instead, it returns a new string which is one of the possible permutations of the string passed to it in the parameter.

Ex 1 :- php generate alphanumeric random string

<?php

$length = 10;
$str = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
 
echo substr(str_shuffle($str), 0, $length);

?>

Result of the above code is:

 G8ckJ1x3VE

Method 2:- Using md5() Function

The md5() function is used to calculate the MD5 hash of a string. Pass timestamp as a argument and md5 function will convert them into 32 bit characters

<?php

echo substr(md5(microtime()), 0, 10); 
echo "<br>";
echo substr(md5(microtime()), 0, 8); 

?>

Result of the above code is:

5fa44a2bbc 

b1c7c213 

Method 3:- To generate random, unique, alphanumeric numbers in PHP

6-digit alphanumeric number:

Using the below given function, you can generate 6 digits random unique alphanumeric numbers in PHP:

function generateRandomString($length = 6) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $result = '';
    for ($i = 0; $i < $length; $i++) {
        $result .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $result;
}

// Usage example:
echo generateRandomString();

8-digit alphanumeric number:

Using the below given function, you can generate 8 digits random unique alphanumeric numbers in PHP:

function generateRandomString($length = 8) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $result = '';
    for ($i = 0; $i < $length; $i++) {
        $result .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $result;
}

// Usage example:
echo generateRandomString();

Recommended PHP Tutorial

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *