PHP Random Quote Generator

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

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

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.

Add new comment

The content of this field is kept private and will not be shown publicly.
2 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.