September 24, 2017

Core PHP: Reading a File (8.5)

Reading a File


The file() function reads the entire file into an array. Each element within the array corresponds to a line in the file:
$read = file('names.txt');
foreach ($read as $line) {
  echo $line .", ";
}

This prints all of the lines in the file, and separates them with commas.
We used the foreach loop, because the $read variable is an array.

No comments:

Post a Comment