PHP feof Test If the file pointer is at the end of the file _php tutorial

Source: Internet
Author: User
EOF is a very important concept, and almost every major programming language provides built-in functions 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 often used in file I/O operations. The form 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
This PHP manual the exact words above.
For convenience, I used to use it this way.
If file can not be read or doesnt exist fopen function returns FALSE
$file = @fopen ("No_such_file", "R");
FALSE from fopen'll issue warning and result in infinite loop here
while (!feof ($file)) {
}
Fclose ($file);
?>

Indeed, 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, there will be a waring, concurrent life cycle.
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 security reasons, it is better to use the above code to add a judgment, is_resource or relatively safe

http://www.bkjia.com/PHPjc/486144.html www.bkjia.com true http://www.bkjia.com/PHPjc/486144.html techarticle EOF is a very important concept, and almost every major programming language provides built-in functions to verify that 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.