The method used by php feof to identify the characters at the end of the file_PHP tutorial

WBOY
Release: 2016-07-21 15:36:13
Original
1094 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
This php manual is above original words.
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, use comparison like this Simple. However, if the above variable $file is not a legal file pointer or has been closed by fclose.
Then in the sixth line of the program, a warning will be generated and an infinite loop will occur.
Why?
The reason is that
Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.
So, for safety reasons, it is best to add a Judgment, is_resource is relatively safe.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322182.htmlTechArticleEOF is a very important concept. Almost every mainstream programming language provides corresponding built-in functions to verify parsing. Whether the processor has reached file EOF. In PHP, this function is feof (). feof...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!