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

Generate 6,8,10 digit random, unique, alphanumeric string 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 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 a random, unique, alphanumeric string in PHP

First Method:- 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

Ex 2 :- php generate alphanumeric random string fixed length

Result of the above code is:

HxJl2r 

qPKEtmkC 

Second Method:- 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 

Recommended PHP Tutorial

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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