Random Quote PHP Script

The script below displays a random string of text each time your page is loaded or refreshed. It is not the most fancy or flexible – but it is extremely simple and easy. This could prove useful if you are doing something with limited scope.

<?php

function randomQuote() {

	$quotes[] = '"Quote goes here"';
	$quotes[] = '"Quote goes here"';
	$quotes[] = '"Quote goes here"';
	$quotes[] = '"Quote goes here"';

	srand ((double) microtime() * 1000000);
	$random_number = rand(0,count($quotes)-1);

	echo '<p>' . ($quotes[$random_number]) . '</p>';
}
?>

Add as many quotes as you like, and you can add HTML and CSS between the quotes. For example, I wanted to provide a link (the author’s name) to more information about the quote or author.

$quotes[] = '"The Quote"<br />&mdash; <a href="URL">Author</a>';

To use this script on your site, include the file in your html document:

<?php include ("includes/quotes.php"); ?>

Then call the randomQuote function in the spot you want the quotes:

<?php randomQuote();?>

You can download a zip file of this demo if you prefer.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • Print
  • NewsVine

Leave a Reply