Google News
logo
PHP Program to Use feof() to read through a file, line by line, until end-of-file is reached
To read through a file line by line until the end-of-file is reached, we can use the `feof()` function in combination with a loop.

The `feof()` function checks if the end-of-file has been reached, and returns `true` if it has, and `false` otherwise.

Here's an example program that demonstrates how to use the `feof()` function to read through a file line by line :
Program :
<?php
$file = fopen("example.txt", "r") or die("Unable to open file!"); // open the file in read mode

while(!feof($file)) { // loop until end-of-file is reached
  $line = fgets($file); // read a single line from the file
  echo $line; // display the line
}

fclose($file); // close the file
?>
In the above program, we opened the file "example.txt" in read mode using the `fopen()` function.

Then we used a while loop to read through the file line by line until the end-of-file is reached. Inside the loop, we read a single line from the file using the `fgets()` function, stored it in the variable `$line`, and displayed it using the `echo` statement. Finally, we closed the file using the `fclose()` function.