The problem with stream_get_line

The PHP function stream_get_line reads a line from a file pointer:

string stream_get_line ( resource $handle , int $length [, string $ending ] )

Note that the $length parameter is not optional, so you have to supply some number at which stream_get_line gives up looking for the end of line. Also note that stream_get_line(), unlike fgets(), does not return the newline in the result.

The reading can end either:

  • because a newline was encountered
  • because $length bytes was read
The problem is, the caller of stream_get_line does not always know which happened.

The caller can not know whether the reading stopped because the line ended or because $length bytes were read

If you decide that at most 100 bytes may be read and stream_get_line() returns a string of 100 bytes, you don't know whether the line continues or that a newline was encountered after exactly 100 bytes.