PHP Math Exercises: Create a human-readable random string for a captcha
PHP math: Exercise-11 with Solution
Write a PHP function to create a human-readable random string for a captcha.
Sample Solution:
PHP Code:
<?php
function random_string($length = 5)
{
$chars = 'bcdfghjklmnpqrstvwxyzaeiou';
for ($x = 0; $x < $length; $x++)
{
$result .= ($x%2) ? $chars[mt_rand(19, 23)] : $chars[mt_rand(0, 18)];
}
return $result;
}
echo random_string();
?>
Sample Output:
rasyn
Flowchart :

PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP function to get random float numbers.
Next: Write a PHP function to get the distance between two points on the earth.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join