Notes for the feof () method of PHP

Source: Internet
Author: User

Introduction: This is a detailed page of the PHP feof () method that requires attention. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 340516 'rolling = 'no'>

I have written such a paragraphCode:

 
If (file_exists ($ pmr_config ["datasetfile"]) {$ tmp_counter = 0; $ Hd = fopen ($ pmr_config ["datasetfile"], "R "); if ($ HD! = False) {While (! Feof ($ HD) {$ buffer = fgets ($ HD); if ($ tmp_counter >=$ SEQ) {$ result [] = $ buffer;} $ tmp_counter ++; if ($ tmp_counter >=$ seq + $ size) {break ;}} else {echo "Warning: Open File {$ pmr_config [" datasetfile "]} failed! Php_eol ";}} else {echo" Warning: file {$ pmr_config ["datasetfile"]} does not exsits! Php_eol ";}

When the number of read rows includes the end of the file, the $ result array always has one more element than expected:

 
(Boolean) False

In other words, if the last row is read, The feof function returns true, and the while loop exits. Why not?

 
While (! Feof ($ HD )){

It turns out to be like this:

 
<? PHP // 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 herewhile (! Feof ($ file) {}fclose ($ file);?>

Feof () is, in fact, reliable.
However, you have to use it carefully in conjunction with fgets (). A common
(But incorrect) approach is to try something like this:

 
<? $ Fp = fopen ("myfile.txt", "R"); While (! Feof ($ FP) {$ current_line = fgets ($ FP); // do stuff to the current line here} fclose ($ FP);?>

The problem when processing plain text files is that
Feof () will not return true after getting the last line of input. You
Need to try to get input _ and fail _ before feof () returns true. You can
Think of the loop above working like this:

* (Merrily looping, getting lines and Processing
Them)
* Fgets used to get 2nd to last line
* Line is processed
* Loop back up -- feof returns false, so do
Steps inside the loop
* Fgets used to get last line
* Line is processed
* Loop back up -- since the last call to fgets
Worked (you got the last line), feof still returns false, so you do the steps
Inside the loop again
* Fgets used to try to get another line (
There's nothing there !)
* Your code doesn' t realize this, and tries
Process this non-existent line (typically by doing the same actions again)
* Now when your code loops back up, feof returns
True, and your loop ends

There's two ways to solve this:

1. You can put an additional test for feof ()
Inside the loop
2. You can move around your cballs to fgets () So
That the testing of feof () happens in a better location

Here's solution 1:

 
<? $ Fp = fopen ("myfile.txt", "R"); While (! Feof ($ FP) {$ current_line = fgets ($ FP); If (! Feof ($ FP) {// process current line} fclose ($ FP);?>

And here's solution 2 (IMHO, more elegant ):

 
<? $ Fp = fopen ("myfile.txt", "R"); $ current_line = fgets ($ FP); While (! Feof ($ FP) {// process current line $ current_line = fgets ($ FP);} fclose ($ FP);?>

FYI, the EOF () function in C ++ works the exact
Same way, so this isn' t just some weird PHP thing...

Ref: http://cn.php.net/manual/en/function.feof.php

Love J2EE follow Java Michael Jackson video station JSON online tools

http://biancheng.dnbcw.info/php/340516.html pageno: 7

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.