random quotes with PHP

So you want to display a randomly selected quote on your page, and you want to use PHP to do it. It’s dang simple, let me tell you.

Requirements

  • You need PHP support on your server. An easy way to test this is to stick <?php phpinfo(); ?> into a .php file, upload that file to your server, and load it in your browser. If you get a huge table with information about your PHP setup, then you know you have PHP support. If you get a blank page, then you don’t have PHP. :(

Howto

  • Create a file with one quote per line. Save it as quotes.txt.

  • The file in which you want to display your random quote must be a PHP file (i.e. it must end in .php). Open this file and stick in the following:
    <?php
    $quotes = "/the/full/path/to/your/file/quotes.txt";
    $quotes = file($quotes);
    srand((double)microtime() * 1000000);
    $ranNum = rand(0, count($quotes)-1);
    ?>

    It doesn’t matter where you put that.
  • In the area where you want your random quote displayed, insert the following:
    <?php echo ($quotes[$ranNum]); ?>

If you want, you may take a look at my random quotes file (if you just want to read the quotes, may I suggest this prettier version).

Other Uses

You don’t have to use this for just random quotes, you know. Maybe you want to display a random image of yourself. You might use the following code:

<?php
$images = "/the/full/path/to/your/file/images.txt";
$images = file($images);
srand((double)microtime() * 1000000);
$ranNum = rand(0, count($images)-1);
echo "<img src=\"";
echo ($images[$ranNum]);
echo "\" alt=\"Random image\">";
?>

And in this case, your images.txt file wouldn’t contain one random quote per line, but one image name per line. Example:
/images/john.jpg

http://www.example.com/tom.png

me.gif

This entry was posted in Programming and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

8 Comments

  1. Posted 9 June 2005 at 7:54 AM | Permalink

    I wrote something very similar a while ago for showing random images/quotes (it’s super basic)..

    $max = count($quote);
    $count = rand(1,$max);

    (and then obviously put the quotes in an array.)

    I remember trying to do this with JavaScript last year and to me at the time it seemed much more complex. I love PHP, it is my friend.

  2. Posted 9 June 2005 at 3:57 PM | Permalink

    This has nothing to do with this entry, but I love the new style!

  3. Posted 11 October 2005 at 12:10 PM | Permalink

    That actually seems to be the best explanation I’ve gotten for the random quote… you’re so awesome!

  4. Christina
    Posted 29 October 2007 at 11:38 PM | Permalink

    For some reason I’m getting all the quotes on the same page, and not one random one. I copied your code exactly, I didn’t alter it at all. Do you maybe know what the problem is? If you do and you can help me, please e-mail me about it. :D

    Thank you for this code though! It was a great help. ;)

  5. Posted 21 February 2008 at 1:44 AM | Permalink

    I have copied and pasted as per the pages, But it don’t work. What format should the location of the file be. Relative to the random files or relative to the site?

  6. Posted 21 February 2008 at 10:12 AM | Permalink

    Tim: the random quotes file should be a plain text file (ending in .txt) with one quote per line. You can put that quotes file anywhere on your site. In your PHP script, you’ll need to specify the full path (not URL, but path on your server) to your quotes file, so something like /home/tim/my_site/quotes.txt

  7. Posted 3 July 2008 at 12:11 AM | Permalink

    This is a great script.

    One problem I noticed that could caused the php to throw back an error … when you copy and paste, depending on your text-editor, it might paste the ” around the txt file reference incorrectly … as right-angle quotes instead of just straight-quotes.

    Thanks!

  8. ill-fated
    Posted 13 September 2009 at 5:18 AM | Permalink

    can you explain the code to me?

    i am a complete newbie in php so mind explaining this to me line by line? whats $quotes srand double macrotime etc etc..

    desperately needing your help..thanks..

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>