w3resource
php Programming Exercies

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?



New Content: Composer: Dependency manager for PHP, R Programming