How to generate random alphanumeric string in PHP?

By Hardik Savani | March 4, 2016 | 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 1

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


Example 2

$generated = rand(10000,99999);


Example 3

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


Example 4

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


Example 5

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


Example 6

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


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.