Tutorial for feof () in Reading file

What is feof () used in Reading file?

The feof() function in PHP is an inherent function that checks a file pointer for the end-of-file. It determines whether or not the “end-of-file” has been reached. If the size of the content of a file is not known ahead of time, the feof() method is used to loop over it.

Why feof () used in Reading file ?

If the end-of-file has been reached or an error has occurred, the feof() method returns True. Otherwise, False is returned.

Syntax:

feof( $file )

Parameters:

In PHP, the feof() method takes only one parameter, $file. The file to be examined for end-of-file is specified by this argument.

Return Value:

If the end of the file has been reached or an error has occurred, it returns TRUE. Otherwise, False is returned.

Errors And Exception:

  • If the supplied file reference is invalid, it enters an infinite loop since end-of-file does not return True.
  • If a connection opened by fsockopen() is not terminated by the server, the feof() function hangs.

Let’s take one Example:-

Below examples will explain the feof() function:

Example 1:- In the below program the file named “test.txt” contains only a single line of text which is “This file consists of only a single line.”.

Output:

Example 2:- In the below program the file named “test-test.txt” contains the following text.

This is the first line.
This is the second line.
This is the third line.

Output:

Tagged : / / / / / / / / / /