Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP fgets() and fgetc() functions

Reference for PHP fgets() and fgetc(): read one line or one byte from an open file handle, the length argument, false at end of file and the classic while loop.

fgets() reads one line from an open file handle and returns it as a string, including the trailing newline. When there is nothing left to read, it returns false. fgetc() does the same for a single byte. Together they are the building blocks for processing a file piece by piece without loading all of it into memory.

The usual pattern is a while loop that stops when fgets() returns false. Always compare with !== false, because a line such as "0" without a newline is falsy but is still data.

PHP
$handle = fopen("data.txt", "r");
while (($line = fgets($handle)) !== false) {
    echo rtrim($line), "\n";
}
fclose($handle);

Try it

PHP Reading a file line by line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Limiting length and reading single characters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
fgets(resource $stream, ?int $length = null): string|false
fgetc(resource $stream): string|false
Name Description
$stream A file handle opened for reading, for example by fopen().
$length Optional. If given, fgets() reads at most $length - 1 bytes. Without it, the whole line is returned, however long.

Return value: fgets() returns the line (with its line ending) or false at end of file or on error. fgetc() returns a one-byte string or false at end of file.

Usage notes

  • The newline is kept. Use rtrim($line, "\r\n") to remove it; plain trim() also removes spaces and tabs you may want to keep.
  • Test with !== false. A final line "0" (no trailing newline) is falsy, so while ($line = fgets($h)) would stop early.
  • $length counts a terminator. fgets($h, 5) returns at most 4 bytes. A long line therefore arrives in several calls.
  • A file that ends with a newline produces no extra empty last line; the next call returns false.
  • Line endings are detected by the line feed character only, so old files that use a bare carriage return come back as one long line.
  • fgetc() reads bytes, not characters. A multibyte UTF-8 letter comes back one byte at a time.
  • Memory. fgets() holds only one line at a time, which makes it the right tool for files larger than memory. See file() for the opposite approach.
  • For CSV do not split lines by hand; use fgetcsv(), which understands quotes and embedded newlines.
  • Do not use feof() as the loop condition. It becomes true only after a read has hit the end, which causes a bogus last iteration. See feof().