Home > Backend Development > PHP Tutorial > every moment of my life php feof method used to identify the characters at the end of the file

every moment of my life php feof method used to identify the characters at the end of the file

WBOY
Release: 2016-07-29 08:43:26
Original
1352 people have browsed it

EOF is a very important concept, and 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 the end of the resource has been reached. It is frequently used in file I/O operations. Its form is:
int feof(string resource)
The example is as follows:

Copy code 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 of this php manual.
For convenience, I used to use it like this before

Copy the code 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, it is easier to use this way. However, if the above variable $file is not a legal file pointer or has been closed by fclose.
Then on the sixth line of the program, a warning will be generated and an infinite loop will occur.
Why?
The reason is
Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.
So, for the sake of safety, it is best to add a judgment when using the above code, is_resource is still safer of.

The above introduces the method used by every moment of my life php feof to identify the characters at the end of the file, including the content of every moment of my life. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template