PHPfeof test whether the file pointer has reached the end of the file _ PHP Tutorial

Source: Internet
Author: User
PHPfeof test whether the file pointer has reached the end of the file. 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 (). Feo 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)
The instance 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.
// If file can not be read or doesnt 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

Http://www.bkjia.com/PHPjc/486144.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486144.htmlTechArticleEOF 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 (). Feo...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.