Content · Programming · PHP

PHP fgets() Function: Usage and Examples

Reads the next line from an open stream.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

fgets() Function is one of the PHP operations used in filesystem and stream workflows. Reads the next line from an open stream. The examples below show what it does and how to handle its result.

What Is fgets() Function?


Reads the next line from an open stream.

Its syntax is:

fgets($stream, ?int $length = null)

The parameters are:

  • $stream — An open stream resource.
  • ?int $length = null — Maximum bytes to read or number of bytes to process.

Return value: The declared return type is string|false. The returned line may include its newline, and false indicates end-of-file or an error.

Important: The returned line may include its newline, and false indicates end-of-file or an error.

Example 1: Basic Usage


The basic example focuses on the behavior of fgets() Function: reads the next line from an open stream. Inputs are defined locally or shown as application-controlled paths.

<?php

$stream = fopen("php://temp", "w+");
fwrite($stream, "First\nSecond\n");
rewind($stream);
echo fgets($stream);
fclose($stream);

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for fgets() Function. Notice the different checks, arguments, or output handling.

<?php

$stream = fopen("php://memory", "w+");
fwrite($stream, "alpha\nbeta\n");
rewind($stream);
while (($line = fgets($stream)) !== false) { echo trim($line) . "\n"; }
fclose($stream);

This variation matters because the returned line may include its newline, and false indicates end-of-file or an error.

Practical Example: Using the Operation in an Application


The following example demonstrates how fgets() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$stream = fopen(__DIR__ . "/records.csv", "rb");
if ($stream === false) { throw new RuntimeException("Cannot read records"); }
try { while (($line = fgets($stream)) !== false) { echo trim($line) . "\n"; } } finally { fclose($stream); }

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Reads the next line from an open stream.
  • Limitation: The returned line may include its newline, and false indicates end-of-file or an error.

Comparison With Related Functions


Function or MethodDifference
fgets() FunctionReads the next line from an open stream.
fread()Reads a requested byte count, whereas fgets() reads a line.

Use fgets() Function for the specific behavior described above. The related operation, fread(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Treating stream or file operations as guaranteed complete on the first call.The returned line may include its newline, and false indicates end-of-file or an error. Handle false returns, partial I/O, stream position and closing resources.
Treating fread() as an interchangeable replacement for fgets() Function.Reads a requested byte count, whereas fgets() reads a line.

Key Takeaways


  • Reads the next line from an open stream.
  • The returned line may include its newline, and false indicates end-of-file or an error.
  • Related operation: fread() — Reads a requested byte count, whereas fgets() reads a line.

fgets() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.