The program requires a standard method to identify when the object ends. this standard is usually called the end of the object or the EOF character. EOF is a very important concept. almost every mainstream programming language provides corresponding built-in functions to verify whether the parser has reached the file EOF. In PHP, this function is feof (). The feof () function is used to determine whether it reaches the end of the resource. It is often used in file I/O operations. The format is:
Int feof (string resource)
Example:
The code is 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
The original words above the php manual.
I used this for convenience.
The code is 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 will issue warning and result in infinite loop here
While (! Feof ($ file )){
}
Fclose ($ file );
?>
Indeed, this is relatively simple to use. However, if the above variable $ file is not a valid file pointer or has been disabled by fclose.
In the sixth line of the program, a waring will be generated, and the concurrent life and death loop will be generated.
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 best to add a judgment when using the above code, is_resource is relatively safe.