How to randomly read line from a file

This is a very basic way to build a ‘dynamic’ content to your site. You can randomly display quotes, testimonials or what ever content you want with a pre-loaded content in a text file.

Let say you want to display a random quote line from a file.

1
2
3
4
5
6
7
8
<?
function randomLine($filename)
{
    $lines = file($filename) ;
    echo $lines[array_rand($lines)] ;
}
randomLine("quotes.txt"); // example code to call function
?>

Also read how to write to a flat file (to make it even more dynamic – can add content via form)

Leave a Reply

Your email address will not be published. Required fields are marked *