Home php How to Generate random a String In PHP

How to Generate random a String In PHP

by Shahriar Sagor

Today now in this example,I will show you how to generate random string in php. From here you can know how to simply and easily generate random string in php.

Now for Example,random function is first argument then how to get character for string and then the second argument is length to string. Now here the bellow example

Example :

function RandomStringGenerator($length) 
    // Variable which store final string 
    $randomString = ""; 
    // small letters, capital letters and digits
    $characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
    // Find the length of created string 
    $len = strlen($characters); 
    // Loop to create random string 
    for ($i = 0; $i < $length; $i++) 
        // Generate a random index to pick 
        $index = rand(0, $len - 1); 
        // Concatenating the character  
        $randomString = $randomString . $characters[$index]; 
    return $randomString; 
$length = 10; 
echo "Random String of length " . $length 
   . " = " . RandomStringGenerator($length); 

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook


You may also like