PHP Random Quote Generator

11th February 2008 - 2 minutes read time

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.

$file= "quotes.txt";
$quotes = file($file);
$randomquote = rand(0, count($quotes)-1);
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.

This is the first quote - Person One
This is the second quote - Person Two

Fill this with your own quotes and you are away.


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

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


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

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

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 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.