Displaying Random Quotes

Nov 7, 2008 | Tags: PHP | del.icio.us del.icio.us | digg Digg

Displaying random quotes is the easiest thing to do with PHP. But in case you don't know how, here it is:

Listing 1: listing-1.php

  1. <?php
  2. function random_quote()
  3. {
  4.     $quotes = array
  5.     (
  6.         "quote 1",
  7.         "quote 2",
  8.         "quote 3",
  9.         "quote 4",
  10.         "quote 5"
  11.     );
  12.    
  13.     $rval = rand(0, count($quotes) - 1);
  14.     print $quotes[$rval];
  15. }
  16.  
  17. /* select random quote and print */
  18. random_quote();
  19. ?>

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000