The problem when processing plain text files is that feof() will not return true after getting the last line of input. You need to try to get input _and fail_ before feof() returns true. You can think of the loop above working like this:

* (merrily looping, getting lines and processing them)* fgets used to get 2nd to last line* line is processed* loop back up -- feof returns false, so do the steps inside the loop* fgets used to get last line* line is processed* loop back up -- since the last call to fgets worked (you got the last line), feof still returns false, so you do the steps inside the loop again* fgets used to try to get another line (but there's nothing there!)* your code doesn't realize this, and tries to process this non-existent line (typically by doing the same actions again)* now when your code loops back up, feof returns true, and your loop ends

There's two ways to solve this:

1. You can put an additional test for feof() inside the loop2. You can move around your calls to fgets() so that the testing of feof() happens in a better location

PHP Warning: Wrong parameter count for feof() in Command line code on line 1PHP Warning: feof(): supplied argument is not a valid stream resource in Command line code on line 1PHP Warning: feof(): supplied argument is not a valid stream resource in Command line code on line 1

Empty: NULLNull: falseUndefined: false

This can, as other commenters have reported, result in infinite loops and massive PHP error logfiles, if the file handle returned by fopen() is invalid for any reason.

Johannes: Remember note from stream_get_meta_data page: For socket streams this member [eof] can be TRUE even when unread_bytes is non-zero. To determine if there is more data to be read, use feof() instead of reading this item.

Another thing: better not rely on the "including socket timeout" part of when feof returns true. Just found program looping two days in while(!feof($fd)) fread ... with 20 seconds timeout in PHP 4.3.10.