PHP Tutorials: How to Generate Random String in PHP?

A random string is a collection of characters. Some times we may need to generate random strings for operations i.e. coupon code, session key, random key etc. In this tutorial I am going to explain you, how we can create a random string in PHP?

I will generate a random string using functions. If you don’t know what a function is then you can read my previous tutorial on Functions. Let us first see the code to generate a random string.

<?php 
//Defining function getRandom with parameter $len and default value 5
function getRandom( $len=5 ) { 
    
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $random_string = ''; 
  
    for ($i = 0; $i < $len; $i++) { 
        $random_index = rand(0, strlen($chars) - 1); 
        $random_string .= $chars[$random_index]; 
    } 

    return $random_string; 
} 

echo '1. Random <strong>6</strong> character string: <strong>'.getRandom(6).'</strong><br>';
echo '2. Random <strong>10</strong> character string: <strong>'.getRandom(10).'</strong><br>';
echo '2. Random <strong>default 5</strong> character string: <strong>'.getRandom().'</strong><br>';
?>
Lakblog: Random String
Lakblog: Random String
  1. Define a function getRandom() with a parameter $len and a default value 5. I took 5 to eliminate any error. If in any case, we don’t pass any value the function still returns a 5 character string.
  2. Take a variable $chars. It has all numbers 0-9, small letters a-z and capital letters A-Z. It will be used as out bucket of character and we will pick characters from this bucket. Keep in mind 1 thing, a string is an array of characters by default. If you echo $chars[0] it will display 0 because $chars have 0 in the first place.
  3. Next comes the loop. The for loop will start from 0 and will loop till it reaches values in $len.
  4. Next, I used a rand() function. It’s an inbuilt function and used to generate a random character or number based on the passed parameters. rand() takes 2 parameters, first is the minimum number and another is the maximum number. Here in rand(), we passed 0 as the minimum and length of the string – 1. Because the index starts from 0 and ends at Length -1.
  5. The generated rand() function will give us a random index number. The index number will be used to pick a character from the bucket.
  6. $random_string is the collector here. Every time we pick a character it will get concatenated with value in $random_string. Read more about concatenation here.
  7. After the loop breaks the function will return the value contained in $random_string.
  8. I called the function with 3 different values. In face 2 values and an empty parameter.

You can manipulate the function as per your need. You can try another way of doing this function to add fun. Make the function to return only number string, a capital letter string or small letter string.

Have fun with coding. If you have any question comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *