1. Home
  2. Computing & Technology
  3. PHP / MySQL

Random Quote Generator

Display random quotes on your website using a PHP switch loop

By Angela Bradley, About.com

One way to display random quotes on your page is by using a switch loop. You can do this by first generating a random number, and then setting each case in the switch loop to echo a different quote.
<?
//chooses a random number
$num = Rand (0,4) ;

//starts the random quote switch loop
switch ($num)
{
case 0:
echo "No soup for you!";
break;

case 1:
echo "Smile!";
break;

case 2:
echo "Time for tea.";
break;

case 3:
echo "Today is your lucky day.";
break;

case 4:
echo "Fead the birds.";
break;
}
?>
You can use more or less quotes by changing the number of cases in the switch loop. Also be sure to change number range for the random number generation.

This entire script can be held in a separate file and called with an include command to the pages you want to display a random quote. You could also put the code directly on the page.

More PHP / MySQL Quick Tips

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Step By Steps
  5. PHP Random Quote - PHP Switch Loop - Random Quote Switch Loop

©2008 About.com, a part of The New York Times Company.

All rights reserved.