EOF is a very important concept, and almost every major programming language provides built-in functions to verify that the parser has reached the file EOF. In PHP, this function is feof (). The feof () function is used to determine whether the end of the resource is reached. It is often used in file I/O operations. The form is:
int feof (string resource)
Examples are as follows:
Copy the Code code as follows:
$fh = fopen ("/home/www/data/users.txt", "RT");
while (!feof ($FH)) echo fgets ($FH);
Fclose ($FH);
?>
BOOL Feof (Resource $handle): Tests for End-of-file on a file pointer
This PHP manual the exact words above.
For convenience, I used to use it this way.
Copy the Code code as follows:
If file can not be read or doesn ' t exist fopen function returns FALSE
$file = @fopen ("No_such_file", "R");
FALSE from fopen'll issue warning and result in infinite loop here
while (!feof ($file)) {
}
Fclose ($file);
?>
Indeed, this is easier to use. However, if the above variable $file is not a valid file pointer or has been closed by fclose.
Then in the sixth line of the program, there will be a waring, concurrent life cycle.
Why?
The reason is
Returns TRUE If the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.
Therefore, for the sake of security, it is better to use the above code to add a judgment, Is_resource is still relatively safe.
The above describes the method of every moment of my life php feof to identify the end-of-file characters, including every moment of my life, and I want to be helpful to my friends who are interested in PHP tutorials.