PHP allows you to easily read your file contents into an array or a string. To read into a string, use file_get_contents() function. To read into an array, use file() function. The file function would copy each line of content into a different array element.
Grocery List
bread eggs milk
grocery_list.txt
PHP code
file() returns contents of file as array file_get_contents() returns contents of file as string Array ( [0] => bread [1] => eggs [2] => milk ) bread eggs milk
<?php
$afile = file('grocery_list.txt');
$sfile = file_get_contents('grocery_list.txt');
print '<br>file() returns contents of file as ' . gettype($afile);
print '<br>file_get_contents() returns contents of file as ' . gettype($sfile);
print '<br>';
print_r($afile);
print '<br>';
print_r($sfile);
?>
grocery_list.php
Output
file() returns contents of file as array file_get_contents() returns contents of file as string Array ( [0] => bread [1] => eggs [2] => milk ) bread eggs milk