PHP feof() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, feof() tells you whether the file pointer of an open file has reached the end of the file. It returns true at the end and false otherwise.

It is the classic way to write a loop that reads a file piece by piece. You keep reading while feof() says there is more, and you stop once the end has been reached.

Syntax

Call feof() with the values you need. Start with this line.

PHP
while (!feof($handle)) { $line = fgets($handle); }
CallWhat it does
feof($handle)Returns true when the end of the file has been reached, false when there is more to read.
while (!feof($handle)) { ... }Loops until the end of the file. Remember to handle a possible false from fgets().
while (($line = fgets($handle)) !== false) { ... }A safer loop that does not need feof() at all.

feof() only becomes true after a read has tried to go past the last byte. If the file ends with a newline, a feof() loop can run one extra time and give you false from fgets().

Library declarations
PHP
feof(resource $stream): bool

Parameters

ParameterDescription
streamRequired. An open file stream created by fopen(), fsockopen() or similar. It must still be open.

Return Value

Return ValueDescription
booltrue if the end of the file was reached or an error occurred, otherwise false.

PHP Examples

Example 1 — Read a File Line by Line

Create a small file and read every line with a feof() loop.

PHP
<?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

  1. implode() joins the three fruit names with line breaks, with no break after the last one.
  2. feof() returns false while there is still data to read.
  3. fgets() reads one line each time, and trim() removes the line break.
  4. After Cherry is read, feof() returns true and the loop ends.

Example 2 — The Extra Loop Pitfall

When the file ends with a newline, the feof() loop runs once more and fgets() returns false. This is a very common bug.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_feof_edge.txt';
file_put_contents($path, 'A' . PHP_EOL . 'B' . PHP_EOL);

$handle = fopen($path, 'r');
$count = 0;

while (!feof($handle)) {
    $line = fgets($handle);
    $count++;
    echo $count, ': ', var_export($line === false ? false : trim($line), true), PHP_EOL;
}

fclose($handle);
unlink($path);

How It Works

  1. The file contains A and B, and each line ends with a newline.
  2. After reading B, PHP has not tried to read past the end yet, so feof() is still false.
  3. The third fgets() call fails and returns false, which sets the end-of-file flag.
  4. Now feof() returns true and the loop stops, but you already got one useless pass.

Example 3 — A Safer Loop and a Final Check

Loop on the value returned by fgets(), then use feof() afterwards to confirm you reached the end.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_feof_safe.txt';
file_put_contents($path, 'One' . PHP_EOL . 'Two' . PHP_EOL . 'Three' . PHP_EOL);

$handle = fopen($path, 'r');
$lines = 0;

while (($line = fgets($handle)) !== false) {
    $lines++;
}

echo 'Lines: ', $lines, PHP_EOL;
echo 'At end: ', var_export(feof($handle), true), PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. fgets() returns a string for each line and false when nothing is left.
  2. The !== false check stops the loop at the right moment, with no extra pass.
  3. $lines counts the three lines that were read.
  4. feof() now returns true because the last read hit the end of the file.

Frequently Asked Questions

It checks whether the file pointer has reached the end of the file. It returns true at the end and false if there is still data to read.
feof() only turns true after a read tries to go past the end. If the file ends with a newline, one more fgets() call runs and returns false. Check the return value of fgets() to avoid this.
For simple cases, a loop with while (($line = fgets($handle)) !== false) is safer. You can also use file() or file_get_contents() when the file is small.
Yes, on network streams such as sockets that stay open without a timeout. Set a timeout with stream_set_timeout() when you read from sockets.
No. Calling feof() on a closed stream throws a TypeError in PHP 8, so only use it while the file is open.

Next: fflush()

fflush() forces any data waiting in the write buffer of an open file to be written out. It.

fflush() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful