Displaying a Random Quote

Browse: Tutorials » PHP
First off we need to create a new blank PHP page so we can make the list of quotes you want to be able to display, you can add as many as you like just by adding a new line.

$quotes[] = "quote 1";
$quotes[] = "quote 2";

We then need to randomly decide which quote to display by using the following code.

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

Next we'll need to add a line to write the selected output to the web page.

echo $quotes[$randomquote];

Lastly we need to add the following line to your PHP page where you want the quote to appear.

<?php include "random_quote.php"; ?>

The complete code

<?php

/*
* Add this line of code in your page:
* <?php include "random_quote.php"; ?>
*/

$quotes[] = "quote 1";
$quotes[] = "quote 2";
$quotes[] = "quote 3";
$quotes[] = "quote 4";
$quotes[] = "quote 5";
$quotes[] = "quote 6";

/* You can continue to add more quotes by
* copying a line and changing the text */

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

echo $quotes[$randomquote];

?>


Thanks for staying tuned.