What is the feof() function in PHP?

feof() is a built-in function in PHP.

We can use the feof() function to check if the ‘end-of-file’ (EOF) character has been found in a specified file.

The EOF character is a special character that marks the end of a file. In most cases, it identifies that the end of a file has been reached while reading data.

The feof() function

Syntax

The following is the function prototype:

 feof(file)

Parameters and return value

The feof() function takes the following input parameters:

  • file: The file to be checked for the EOF character.

The function returns TRUE if the EOF character is found or an error occurs. Otherwise, the function returns FALSE.

main.php
Edpresso.txt
<?php
# open a file
$file = fopen("Edpresso.txt", "r");
# using feof()
while (!feof($file))
{
echo fgets($file); # read a line from the file
}
# close file
fclose($file);
?>

In the example above, we use feof() as our condition in a while loop.

The loop continues to read and display a line from the file until the EOF character is found. In such a case, the feof() function returns TRUE. The loop breaks because of the ! operator, which flips the output to FALSE.

This allows us to read all the data from the file.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved