Example 1 — Read a File Line by Line
Create a small file and read every line with a feof() loop.
<?php
$path = sys_get_temp_dir() . '/codetofun_feof.txt';
file_put_contents($path, implode(PHP_EOL, ['Apple', 'Banana', 'Cherry']));
$handle = fopen($path, 'r');
while (!feof($handle)) {
$line = fgets($handle);
echo trim($line), PHP_EOL;
}
fclose($handle);
unlink($path); How It Works
- implode() joins the three fruit names with line breaks, with no break after the last one.
- feof() returns false while there is still data to read.
- fgets() reads one line each time, and trim() removes the line break.
- After Cherry is read, feof() returns true and the loop ends.

