PHP | feof( ) Function

The feof() function in PHP is an inbuilt function which is used to test for the end-of-file on a file pointer. It checks if the “end-of-file” has been reached or not. The feof() function is used for looping through the content of a file if the size of content is not known beforehand.

The feof() function returns True if end-of-file has been reached or if an error has occurred. Else it returns False.

Syntax:

feof( $file )

Parameters: The feof() function in PHP accepts only one parameter which is $file. This parameter specifies the file which has to be checked for end-of-file.

Return Value: It returns TRUE if end-of-file has been reached or if an error has occurred. Else it returns False.

Errors And Exception:

  1. It goes in an infinite loop if the passed file pointer is not valid, because end-of-file fails to return True.
  2. feof() function hangs if a connection opened by fsockopen() isn’t closed by the server.

Below programs illustrate the feof() function:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// a file is opened using fopen() function
$check = fopen("singleline.txt", "r");
  
$seq = fgets($check);
  
// Outputs a line of the file until
// the end-of-file is reached
while(! feof($check))
{
  echo $seq ;
  $seq = fgets($check);
}
  
// file is closed using fclose() function
fclose($check);
  
?>

chevron_right


Output:

This file consists of only a single line.

Program 2: In the below program the file named “gfg.txt” contains the following text.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// a file is opened using fopen() function
$check = fopen("gfg.txt", "r");
$seq = fgets($check);
  
// Outputs a line of the file until
// the end-of-file is reached
while(! feof($check))
{
  echo $seq ;
  $seq = fgets($check);
}
  
// file is closed using fclose() function
fclose($check);
  
?>

chevron_right


Output:

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

Reference:
http://php.net/manual/en/function.feof.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.