PHP: Random Quote

If you’re looking for a simple “random quote” to add on your site, this is for you. You will need to be able to use PHP on your website and have pages with a .php extension.

Paste the following code into a fresh, new document.

$quotes[] = "Quote here";
$quotes[] = "Quote here";
$quotes[] = "Quote here";
$quotes[] = "Quote here";
$quotes[] = "Quote here";
$quotes[] = "Quote here";
srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);
echo " " . $quotes[$randomquote] . " ";

Save the document as quotes.php.

Editing the code

Replace Quote here with your quote. You can use HTML such to make part of the font bold or use a line break to make a new line.

If you want more quotes, copy the line $quotes[] = "Quote here";.

If you want less quotes, simply delete one of the lines.

Note: If you would like to use double quotes () within your quotes, place a backslash (\) before it. For example:

$quotes[] = "Jim said, \"Gosh you are right!\"";

Or you can just use single quotes.

Putting it on your page

Find the page where you want the random quote to appear.

Simply paste this PHP include:

<?php include('quotes.php'); ?>

Upload both files to your website and it should work.