Random Array Items From Flat Text File Database Tutorial
In this exercise we put several built in PHP functions to use in order to turn an idea into an application. We take the described logic and translate it into script by accessing knowledge we have of PHP's built in functions and applying them. Some people have to use text files in place of MySQL for data management so this lesson is another one that deals with text file data handling for dynamic purposes on a website.
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<html>
<body>
<h2><?php echo $randPhrase; ?></h2>
</body>
</html>
flatFileDB.txt
Hello and Welcome!
Hi how are you today?
Welcome Pilgrims!
Hey There Weenis!
Howdy Partner!
Hola Amigos!
Hallo Vrienden!
Bonjour Les Amis!
Cairde Dia duit!
Cras Venenatis!