EOF is a very important concept, and almost every major programming language provides the corresponding built-in function 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 used frequently in file I/O operations. In the form of:
int feof (string resource)
Examples are as follows:
Copy Code code as follows:
<?php
$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 be like this before.
Copy Code code as follows:
<?php
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
while (!feof ($file)) {
}
Fclose ($file);
?>
Sure, 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, it will produce a waring, concurrent life and death cycle.
Why?
The reason is
Returns TRUE If the file pointer is in EOF or an error occurs (including socket timeout); otherwise returns FALSE.
Therefore, for the sake of safety, it is best to use the above code to add a judgment, Is_resource is still relatively safe.