This tutorial will show you how to retrieve a random line from a file which can be used for random quotes, random ads and other misc purposes
Code pasted for ease of use:
Lets get started!
First, here is the completed code.
PHP Code Box
[php]<?php $file = file('file.txt'); $rand_line = rand(0, sizeof($file) - 1); $line = $file[$rand_line]; echo $line; ?>
Now, we will break it down.
The file() function in php opens a file with each line as an array.
Next, we use rand(START, END) to generate a random number which will be used later to open a random line.
Next, with $file[$rand_line] we call the file, which was stored in an array, and open open the value at the random line key.
All we do next is echo it.