Skip to main content

Generate Random String or Password Using PHP

If you want to generate random numbers, it is possible by using PHP’s rand function. But if you want to generate random string, it is not possible by using rand function. Because rand function only returns numbers as integer between two values.  I coded a simple PHP function that works exactly same as rand function but the deference is it returns string. You can use it in image captcha function, random password generator or anywhere else.

randString Function

* Random String Generator
* @param optional int $min
* @param optional int $max
* @return string

function randString($min = 5, $max = 8){
 # get random character length between minimum and maximum length
 $length = rand($min, $max);
 $string = '';
 # character index [0-9a-zA-Z]
 $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 # loop random times defined by $length
 for ($i=0; $i < $length; $i++) {
  # get random character index
  $string .= $index[rand(0, strlen($index) -1)];
 return $string;


echo randString(4, 8);


Tips: You can remove vowels (a, e, i, o, u) from alphabet index. It will help to generate meaningless string and it will also help to prevent from bad words. Also you can add some special characters (like @, #, $, % & ...). It help to generate strong password.


  1. Nice script! Thanks a lot!

  2. Dapatkan disini , karena Obat Pelangsing slimming Capsule Di Apotik belum tersedia , karena slimming capsule tidak di jual di sembarang temapat termasuk di apotik pun tujuannya untuk menjaga ke aslian produk tersebut, karena sekarang banyak perusahaan yang meniru produk produk herbal sehingga herbal yang asli khasiatnya bisa di rusak oleh produk yang palsu. Untuk itu hati hati dengan slimming capsule

    and obat vig power capsule

  3. I have read your blog its very attractive and impressive. I like it your blog.

    PHP Training in chennai | PHP Training Course

    PHP Training in chennai | Online PHP Course

  4. try this simple code :


Post a Comment

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery . Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar. We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code . multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin . It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.