Creating a Random Quote Script

Posted on August 29, 2008, Filled under PHP,  Bookmark it

This is a Random Quote Script.

<?php
// Credits: http://www.bitrepository.com/

srand((float) microtime() * 10000000); // IF PHP Version < 4.2.0

// Create the array first
$quotes = array();

// Fill it with quotes

$quotes[] = array('quote'  => 'Time is money.',
                  'author' => 'Benjamin Franklin');

$quotes[] = array('quote'  => "And in the end, it's not the years
in your life that count.
 It's the life in your years.",
                  'author' => 'Abraham Lincoln');

$quotes[] = array('quote'  => "The secret to creativity is knowing
how to hide your sources.",
                  'author' => 'Albert Einstein');

$quotes[] = array('quote'  => "A mind that is stretched by a new experience
 can never go back to its old dimensions.",
                  'author' => 'Oliver Wendell Holmes');

/*
NOTE: To add more quotes just continue filling the array like this:

$quotes[] = array('quote'  => "THE QUOTE HERE",
                  'author' => "QUOTES'S AUTHOR");

*/

$rand_key = array_rand($quotes, 1);

$quote_array = $quotes[$rand_key];

$quote = $quote_array['quote'];
$author = $quote_array['author'];

echo $quote.' - '.$author;
?>

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

Sponsors

2 Replies to "Creating a Random Quote Script"

  1. I doubt many people will be using a version of PHP lower than 4.2.

  2. Yes, but it’s good to be there just in case. A good example can be seen at this address: http://www.php.net/array_rand.

Leave a Reply


* = required fields

(will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


  

CommentLuv Enabled