PHP fgets() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fgets() reads one line from an open file. It returns the line as a string, including the line break at the end, or false when nothing is left to read.

Call it again and again to move through a text file line by line. It is the most common function for reading log files, text lists and any file you want to process one line at a time.

Syntax

Call fgets() with the values you need. Start with this line.

PHP
$line = fgets($handle);
CallWhat it does
fgets($handle)Reads the next full line, including the newline character.
fgets($handle, 6)Reads at most 5 bytes (length minus one) or up to the end of the line.
while (($line = fgets($handle)) !== false) { ... }Reads the whole file line by line.

The returned string still contains the line break, so use trim() or rtrim() when you print or compare it. When you pass a length, fgets() reads at most length - 1 bytes.

Library declarations
PHP
fgets(resource $stream, ?int $length = null): string|false

Parameters

ParameterDescription
streamRequired. An open file stream, such as the one returned by fopen(). It must be open for reading.
lengthOptional. Reading stops after length - 1 bytes. The default null reads until the end of the line, with no limit.

Return Value

Return ValueDescription
stringThe line that was read, including the line break if there is one.
falseReturned at the end of the file or on failure.

PHP Examples

Example 1 — Read the First Line

Write three lines to a file and read only the first one.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgets.txt';
file_put_contents($path, 'Apple' . PHP_EOL . 'Banana' . PHP_EOL . 'Cherry' . PHP_EOL);

$handle = fopen($path, 'r');
$line = fgets($handle);

echo 'First line: ', trim($line), PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. file_put_contents() creates a file with three fruit names, one per line.
  2. fopen() opens the file for reading.
  3. fgets() reads only the first line, which is Apple plus its line break.
  4. trim() removes the line break before the text is printed.

Example 2 — Limit How Much Is Read

The second argument limits the bytes that are read. fgets() returns length minus one bytes at most.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgets_edge.txt';
file_put_contents($path, 'Hello World');

$handle = fopen($path, 'r');

echo '[', fgets($handle, 6), ']', PHP_EOL;
echo '[', fgets($handle, 6), ']', PHP_EOL;
echo '[', fgets($handle, 6), ']', PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. The file has a single line, Hello World, with no line break.
  2. With a length of 6, fgets() reads at most 5 bytes, so the first call gives Hello.
  3. The second call continues where the first stopped and returns a space plus Worl.
  4. The third call returns the last letter d, because the end of the file is reached.

Example 3 — Number Every Line

Loop over the whole file and print each line with a line number.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgets_loop.txt';
file_put_contents($path, 'Red' . PHP_EOL . 'Green' . PHP_EOL . 'Blue' . PHP_EOL);

$handle = fopen($path, 'r');
$n = 0;

while (($line = fgets($handle)) !== false) {
    $n++;
    echo $n, ': ', rtrim($line), PHP_EOL;
}

fclose($handle);
unlink($path);

How It Works

  1. fgets() returns a line on each call and false when the file is finished.
  2. The !== false check ends the loop cleanly, with no extra pass.
  3. $n goes up by one for every line that is read.
  4. rtrim() removes the line break so each line prints neatly.

Frequently Asked Questions

It reads one line from an open file and returns it as a string. It returns false when there is nothing left to read.
Yes. The returned string ends with the line break, so use trim() or rtrim() to remove it before printing or comparing.
Use while (($line = fgets($handle)) !== false) { ... }. The loop ends when fgets() returns false at the end of the file.
It limits the read to length - 1 bytes. For example, fgets($handle, 6) reads at most 5 bytes, or fewer if the line is shorter.
fgets() reads one line at a time and uses little memory. file() loads every line into an array at once, which is simpler for small files.

Next: fgetss()

fgetss() used to read one line from a file and strip the HTML and PHP tags from it. The fu.

fgetss() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful