1. Computing & Technology

Discuss in my forum

Random Quote Generator

Display random quotes on your website using a PHP switch loop

By , About.com Guide

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.

©2012 About.com. All rights reserved.

A part of The New York Times Company.