Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 911667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:14:53+00:00 2026-05-15T17:14:53+00:00

I come from a C and C++ background but also play with some web

  • 0

I come from a C and C++ background but also play with some web stuff. All us C folks (hopefully) know that calling feof on a FILE* before doing a read is an error. This is something that stings newbies to C and C++ very often. Is this also the case for PHP’s implementation?

I figure it has to be because the file could be a socket or anything else where it is impossible to know the size before finishing reading. But just about every PHP example (even those found on php.net I’ve seen looks something like this (and alarms go off in my head):

$f = fopen("whatever.txt", "rb");
while(!feof($f)) {
    echo fgets($f);
}
fclose($f);

I know it is preferable to write this like this and avoid this issue:

$f = fopen("whatever.txt", "rb");
while($line = fgets($f)) {
    echo $line;
}
fclose($f);

but that’s besides the point. I tried testing if things would fail if I did it "the wrong way", but I could not get it to cause incorrect behavior. This isn’t exactly scientific, but I figured it was worth a try.

So, is it incorrect to call feof before an fread in PHP?

There are a couple of ways that PHP could have done this differently that C’s version, but I feel they have downsides.

  • they could have it default to !EOF. This is sub-optimal because it may be incorrect for some corner cases.

  • they could get the file size during an fopen call, but this couldn’t work on all types of file resources, yielding inconsistent behavior and would be slower.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:14:55+00:00Added an answer on May 15, 2026 at 5:14 pm

    PHP doesn’t know whether it’s at the end of the file until you’ve tried to read from it. Try this simple example:

    <?php 
    $fp = fopen("/dev/null","rb"); 
    while (!feof($fp)) { 
        $data = fread($fp, 1);
        echo "read " . strlen($data) . " bytes";  
    } 
    fclose($fp); 
    ?>
    

    You will get one line reading read 0 bytes. feof() returned true even though you were technically at the end of the file. Usually this doesn’t cause a problem because fread($fp, 1) returns no data, and whatever processing you’re doing on that handles no data just fine. If you really do need to know if you’re at the end of the file, you do have to do a read first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from Java Background and so used to Debugging using Eclipse but have
I'm new to PHP but come from a Java background and I'm trying to
I come from a .NET background and need to do a web project in
I come from a PHP background and I'm just getting my teeth into some
Though I come from a purely PHP background on the web development side of
I come from a C/C++ background and am having trouble doing some things in
I come from a PHP background and I'm used to calling a class' internal
Background I've picked up some incomplete work from another developer that involves displaying the
I'm relatively new to C# - but come from a C/C++ background. I need
I come from a C++/Java background, but I'm having problems getting the syntax right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.