0

I have read the comment about feof() on PHP manual, the answer here on StackOverflow and another answer on StackOverflow.

So I've understood that using feof() does not mean using a 'crystal ball' to magically know whether there are more characters or lines in a stream, as mentioned second link above. Rather, it is a test that indicates whether an input operation failed or not (in this case, I only care about the input operation of fgetc() of PHP). So the last character read will still be okay. AND THEN, the feof() of while(!feof($file_you_read_from)) will still be false simply because the last fgetc() still succeeded and after one more loop where fgetc() attempts to read and fail, only then will feof() be set to true. Am I correct? Did I misunderstand something?

Richard
  • 5,945
  • 7
  • 37

1 Answers1

1

feof in PHP works the same as C.

If it is false then it checks the last operation otherwise returns the cached value.

Source is here

Comment for C's feof

Checks whether the end-of-File indicator associated with stream is set, returning a value different from zero if it is.

This indicator is generally set by a previous operation on the stream that attempted to read at or past the end-of-file.

Notice that stream's internal position indicator may point to the end-of-file for the next operation, but still, the end-of-file indicator may not be set until an operation attempts to read at that point.

This indicator is cleared by a call to clearerr, rewind, fseek, fsetpos or freopen. Although if the position indicator is not repositioned by such a call, the next i/o operation is likely to set the indicator again.

shingo
  • 6,928
  • 4
  • 13
  • 29
  • I see. So my assumption is correct: that the value of `feof()` is set by the **latest** operation of read, i.e. the `fgetc()` I mentioned, right? – Richard Feb 13 '19 at 07:41
  • A bit wrong, it affects by the latest opertion but set by `feof`, you can check the source code. – shingo Feb 13 '19 at 07:49
  • Do you mean this one: https://github.com/php/php-src/blob/PHP-7.4/main/streams/streams.c#L727? Admittedly, I have some trouble understanding that section. – Richard Feb 13 '19 at 07:56
  • So that means, it is set by `feof()`, and `feof()` knows which value to set by getting the status of the last operation of read, right? – Richard Feb 13 '19 at 08:44