Program to generate random string in PHP Read Courses Improve Improve Improve Like Article Like Save Article Save Report issue Report Given a size N and the task is to generate a random string of size N. Examples: Input: 5 Output: eR3Ds Input: 10 Output: MPRCyBgdcn Method: Create a domain string which contains small letters, capital letters and the digits (0 to 9). Then generate a random number and pick the character present at that random index and append that character into the answer string. Below is the program to generate random string using above method: <?php // PHP function to print a // random string of length n function RandomStringGenerator($n) { // Variable which store final string $generated_string = ""; // Create a string with the help of // small letters, capital letters and // digits. $domain = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // Find the length of created string $len = strlen($domain); // Loop to create random string for ($i = 0; $i < $n; $i++) { // Generate a random index to pick // characters $index = rand(0, $len - 1); // Concatenating the character // in resultant string $generated_string = $generated_string . $domain[$index]; } // Return the random generated string return $generated_string; } // Driver code to test above function $n = 5; echo "Random String of length " . $n . " = " . RandomStringGenerator($n); ?> Output: Random String of length 5 = EEEto Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now! Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 27 Nov, 2019 Like Article Save Article Previous How to get random value out of an array in PHP? Next How to calculate the difference between two dates in PHP ? Share your thoughts in the comments Add Your Comment Please Login to comment...