random quotes with PHP
So you want to display a randomly selected quote on your page, and you want to use PHP to do it; you’ll need PHP support on your server. An easy way to test this is to stick <?php phpinfo(); ?>
into a .php file, upload that file to your server, and load it in your browser. If you get a huge table with information about your PHP setup, then you know you have PHP support. If you get a blank page, then you don’t have PHP. :(
Create a file with one quote per line and save it as quotes.txt
.
The file in which you want to display your random quote must be a PHP file. Add the following code to it:
1 2 3 |
$quotes = file("/the/full/path/to/your/file/quotes.txt"); srand((double)microtime() * 1000000); $ranNum = rand(0, count($quotes)-1); |
In the area where you want your random quote displayed, insert the following:
1 |
echo $quotes[$ranNum]; |
You don’t have to use this for just random quotes either – maybe you want to display a random image. You might use the following code:
1 2 3 4 |
$images = file("/the/full/path/to/your/file/images.txt"); srand((double)microtime() * 1000000); $ranNum = rand(0, count($images)-1); echo '<img src="' . $images[$ranNum] . '" alt="Random image">'; |
And in this case, your images.txt
file wouldn’t contain one random quote per line, but one image name per line:
/images/john.jpg
http://www.example.com/tom.png
me.gif