How to generate random alphanumeric string in PHP?

By Hardik Savani | March 4, 2016 | | 37975 Viewer | Category : PHP


In this example i am going to show you how to generate randomly alphanumeric string OR number in PHP. when you need you generate random alphanumeric string for your unique field then you can easily create using substr(), md5(), rand(), uniqid(), mt_rand(), time() and str_shuffle() php function you can create several way unique code using following example:

Example

Example 1

$generated = substr(md5(rand()),0,5);

print_r($generated);

Example 2

$generated = rand(10000,99999);

print_r($generated);

Example 3

$generated = substr(md5(uniqid(rand(), true)),0,5);

print_r($generated);

Example 4

$generated = substr(time(),5);

print_r($generated);

Example 5

$generated = substr(str_shuffle(md5(time())),0,5);

print_r($generated);

Example 6

$generated = substr( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,mt_rand( 0 ,50 ) ,2 ) .substr( md5( time() ), 1,3);

print_r($generated);



Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. 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, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.