PHP Random Quote Generator

11th February 2008

The following code loads the contents of a text file and randomly displays a line from it. You can use this to display a random quote on a page every time it loads.

  1. $file= "quotes.txt";
  2. $quotes = file($file);
  3. srand((double)microtime()*1000000);
  4. $randomquote = rand(0, count($quotes)-1);
  5. echo $quotes[$randomquote];

It works by loading a file into memory and picking a line from that file at random. Here is a sample file you might want to use.

  1. This is the first quote - Person One
  2. This is the second quote - Person Two

Fill this with your own quotes and you are away.

Comments

Permalink
im not too good at html but this is a good code

Emmie (Mon, 01/03/2011 - 23:36)

Permalink

Very small but simple little snippet that did just what was needed. Thanks

bernard (Sun, 10/16/2011 - 20:32)

Permalink
This is exactly what I wanted in a Random Quote Generator, but it's not working quote right for me. It's pulling in what I assume is the generated random number and putting it in the quote text. Any way to block that? See http://genealogy.ravensbeak.com/ for an example.

cmhbob (Wed, 06/06/2012 - 20:19)

Add new comment

The content of this field is kept private and will not be shown publicly.