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

PHP Random Quote

By Angela Bradley, About.com

Using Switch to Display Random Quotes: This code demonstrates how to use switch () to display a random quote on your website. Each of our quotes is set to run in the case that it's number is chosen. By using rand () to choose a random number, we are able to display one of the quotes at random.
<?
//Chooses a random number
$num = Rand (1,6);
//Based on the random number, gives a quote
switch ($num)
{
case 1:
echo "Time is money";
break;
case 2:
echo "An apple a day keeps the doctor away";
break;
case 3:
echo "Elmo loves dorthy";
break;
case 4:
echo "Off to see the wizard";
break;
case 5:
echo "Tomorrow is another day";
break;
case 6:
echo "PHP is cool!";
}
?>
To add more quotes you would simply change the rand () function to allow for higher numbers, and then add in their corresponding cases below.

If you wanted to include a random quote on your PHP web page, you could simply use include () to pull the quote from this file, like this:

INCLUDE 'http://www.yoursite.com/path/to/quote_file.php'
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.