• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Random Quotes

Moonman

Well-Known Member
NLC
I have this list of facts, and i want one of them randomly displayed whenever someone visits my site, how would i do this in PHP?
 
Code:
<?php

$quotes = Array(
'quote one',
'quote two',
'another quote',
'and so on',
'last one'
);

srand((double)microtime()*1000000);

$count = count($quotes) - 1;
$randn = rand(0, $count);

echo $quotes[$randn];

?>
If your quotes are in a text file, each quote on their own line...

Code:
<?php

$file = '/path/to/quotes.txt';
if(file_exists($file))
{
	$open = fopen($file, 'r');
	$read = fread($open, filesize($file));
} else {
	die('File Does Not Exist');
}

$quotes = explode("\n", $read);

srand((double)microtime()*1000000);

$count = count($quotes) - 1;
$randn = rand(0, $count);

echo $quotes[$randn];

?>
*note: I typed the above very quickly...
 
Last edited:
Back
Top