How to generate random alphanumeric string in PHP?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello All, Today now in this blog i will show you how to generate random alphanumeric string in php . So now in this example i am going to show you how we can generate randomly alphanumeric string OR number in PHP application. When we are working any project maximum time we need generate random alphanumeric string for our unique field then we can easily create this by using substr(), md5(), rand(), uniqid(), mt_rand(), time() and str_shuffle() or other php function we can create this on several way unique code by using the 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);

Read Also : How to Create Custom Blade Directive in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →