Random quote
Views Today: 1, Total Views: 3793, Submitted by: magnus on 21-06-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.
Quote
<?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.
User Comments
If you want to comment on this tutorial you must first register or login.
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/cinedict/public_html/tutorialdash/tutorialview.php on line 287
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/cinedict/public_html/tutorialdash/tutorialview.php on line 287
I dont see what this line is for, or what it does:
srand(time());
Can someone explain..?
- CBG
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/cinedict/public_html/tutorialdash/tutorialview.php on line 287
I dont see the significance of:
srand(time());
Can someone please explain it?
Thanks, CBG.
Post A Comment
Fill out the following form to comment on this tutorial.



Syndicate
Very simple and to the point. Thanks