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.
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 1 :- 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