Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Generate Random Quotes with PHP

By Darren W. Hedlund
2005-03-30


Generate Random Quotes with PHP

The following will show you how to generate simple random quotes using PHP.

The first step it to open the PHP script:

<?

Next we will need to open the array sequence.

$quote = array(

You can have as many as you want, just open each one as a new number:

1  => "a",

2 => "b",
3 => "c",
4 => "d",
5 => "e",
Now close the array:

);

Now set up the random sequence. Notice that rand (1,5) meaning 1 through 5. you could change this to be 3,5 and it would only use array 3, 4 and 5:

srand ((double) microtime() * 1000000);

$randnum = rand(1,5);
Notice that the array uses $quote, and the random sequence uses $randnum, so we need to bind those two together:

echo"$quote[$randnum]";

Now end the PHP script:
?>

Here is the complete code:

<? 

$quote = array(
1 => "a",
2 => "b",
3 => "c",
4 => "d",
5 => "e",
);
srand ((double) microtime() * 1000000);
$randnum = rand(1,5);
echo"$quote[$randnum]";
?>


Tutorial Pages:
» Generate Random Quotes with PHP


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.