Every moment of my life php feof methods used to identify the end character of a file

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)
Examples are as follows:

Copy the Code code 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.

Copy the Code code 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'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.
Therefore, for the sake of security, it is better to use the above code to add a judgment, Is_resource is still relatively safe.

The above describes the method of every moment of my life php feof to identify the end-of-file characters, including every moment of my life, and I want to be helpful to my friends who are interested in PHP tutorials.

  • 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.