Display Random Quotes with PHP

The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart. - Hellen Keller

This is a simple way to display random text, such as quotes on your web page using PHP. Each time the page is refreshed, the script will choose a quote to display on your page.

What you need

Your webserver must be able to run PHP scripts.

Step 1

1. Create a page and save it as, for example, quotes.php

2. At the very top of your page add the following:

<?php
// Random Quote Script by www.paintedpixels.com

// add quotes here
$quoteArray[0] = "The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart. - Hellen Keller";
$quoteArray[1] = "Some cause happiness wherever they go; others whenever they go. - Oscar Wilde";
$quoteArray[2] = "Money talks...but all mine ever says is good-bye. - Anon";

// get a random quote
$quote = trim($quoteArray[rand(0, count($quoteArray) - 1)]);
?>

What does this mean?

$quoteArray[0] = "The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart. - Hellen Keller";

This sets up the quotes in an array. You don't have to understand this. All you really have to understand is that you can put anything between the quotes to display your own text.

If you wish to add more quotes continue adding lines like this:

$quoteArray[3] = "He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever. - Chinese Proverb";
$quoteArray[4] = "Use what talent you possess: the woods would be very silent if no birds sang except those that sang best. - Henry Van Dyke";
$quoteArray[5] = "We are what we think. All that we are arises with our thoughts. With our thoughts, we make the world. - Buddha";

Notice how the format stays the same but the number in square brackets increases by one each time.

$quote = trim($quoteArray[rand(0, count($quoteArray) - 1)]);

The line above randomly chooses one of the quotes from the array.

Step 2

Create your page as normal after the ?>. For example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Quotes</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

</body>
</html>

Step 3

Decide where you want your quote to appear on your page and add the following to that place:

<?php echo $quote;?>

You are now finished!

The complete code should look like this:

<?php
// Random Quote Script by www.paintedpixels.com

// add quotes here
$quoteArray[0] = "The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart. - Hellen Keller";
$quoteArray[1] = "Some cause happiness wherever they go; others whenever they go. - Oscar Wilde";
$quoteArray[2] = "Money talks...but all mine ever says is good-bye. - Anon";
$quoteArray[3] = "He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever. - Chinese Proverb";
$quoteArray[4] = "Use what talent you possess: the woods would be very silent if no birds sang except those that sang best. - Henry Van Dyke";
$quoteArray[5] = "We are what we think. All that we are arises with our thoughts. With our thoughts, we make the world. - Buddha";

// get a random quote
$quote = trim($quoteArray[rand(0, count($quoteArray) - 1)]);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Quotes</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<h2 align="center">Display Random Quotes with PHP</h2>
<p align="left"><?php echo $quote;?></p>

</body>
</html>

Now when you refresh the page, a new quote will be displayed. Enjoy!