CodeCrunch - http://www.codecrunch.com
Random Quotes with PHP
http://www.codecrunch.com/articles/46/1/Random-Quotes-with-PHP.html
By Brendan Horverson
Published on 06/23/2006
 
This tutorial will show you how to make random quotes appear. This can also be used for other things like random pictures, links and everything you can store in an array.

Radmon Quotes with PHP

<?PHP

$quotes = array(
"This is my quote",
"To be or not to be",
"<b>You can</b> <u>use html</u>");

$count = count($quotes);

srand(time());
$random = (rand()%$count);

echo $quotes[$random];

?>

 

The first part creates an array named $quotes.

After that comes $count = count($quotes);. It counts how many entries there are in the $quotes-array and stores it in a variable named $count. We are using this var in the next step.

the srand(time());
$random = (rand()%$count); part creates a random number and stores it in a variable named $random. You can see that I am using the $count-variable. In this example it is 3. That is because of the 3 entries in the array that $count = count($quotes); counted.

The next part prints out a random entry from the array we created at the top. $random here equals a random number between 0-2. 0, 1, 2. If you didn't want it random you could write something like echo $quotes['2'];. That would print the last line of the array. Yes the third line. The counting starts at 0. 0,1,2,3,4,5.

If you want to print a random picture just put
<img src='address_to_image.jpg' alt='my picture'> in the array.