w3resource

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 :

Flowchart: Create a human-readable random string for a captcha

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.



Follow us on Facebook and Twitter for latest update.