Friday 10 August 2018

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

1 comment: