Tutorial Categories
Tools and Software
Site Search


Advanced Search
Tutorial Options
Popular Tutorials
  1. Photoshop Concept Car Makeover
  2. Photoshop Man Of Fire Part I
  3. Photoshop Man Of Fire Part II
  4. Photoshop Magic Marker Effect
  5. Photoshop Car Makeover Part II
No popular tutorials found.
Popular Authors
  1. Cory Crampton
  2. Daniel Phillips
  3. tanmay goswami
  4. Rails Forum
  5. Brendan Horverson
  6. CodeCrunch Tutorial Bot
  7. PhotoshopBee .com
  8. Jamie Lewis
  9. Nick Cote
  10. Luther Avery
No popular authors found.
 »  CodeCrunch  »  Programming  »  PHP Tutorials  »  Introduction to PHP  »  Random Quotes with PHP
Random Quotes with PHP
By Brendan Horverson | Published  06/23/2006 | Introduction to PHP | This tutorial viewed 815 times
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.

 




How would you rate the quality of this tutorial?
Add comment
Comments
  • Comment #1 (Posted by Graham)

    Exactly what I was looking for. Thanks.
     
Submit Comment