Content · Programming · PHP

PHP gzeof() Function: Usage and Examples

Understand PHP gzeof() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use gzeof() when you need to tests whether a gzip stream has reached end of file in a PHP application. The examples below show the actual operation and its limitations.

What Is gzeof() in PHP?

Tests whether a gzip stream has reached end of file. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

gzeof($stream): bool

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"OK"); gzclose($file); $file = gzopen($path,"rb"); gzread($file,99); var_dump(gzeof($file)); gzclose($file); unlink($path);

Output:

bool(true)

Example 2: Different Input or Behavior

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"123"); gzclose($file); $file = gzopen($path,"rb"); echo gzeof($file) ? "End" : "Not end"; gzclose($file); unlink($path);

Output:

Not end

Practical Example: Applying the Function

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"line
"); gzclose($file); $file = gzopen($path,"rb"); while (!gzeof($file)) { $chunk = gzread($file,16); if ($chunk === false || $chunk === "") break; echo $chunk; } gzclose($file); unlink($path);

Output:

line

Important Behavior and Edge Cases

Check read failures separately from EOF; do not loop forever on an empty read.

Comparison With Related Functions

ApproachDifference
gzeof()Tests whether a gzip stream has reached end of file.
Related operationfeof() applies to ordinary streams; gzeof() applies to gzip resources.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationCheck read failures separately from EOF; do not loop forever on an empty read.
Confusing this function with a related operationfeof() applies to ordinary streams; gzeof() applies to gzip resources.

Key Takeaways

  • Tests whether a gzip stream has reached end of file.
  • Check read failures separately from EOF; do not loop forever on an empty read.
  • feof() applies to ordinary streams; gzeof() applies to gzip resources.

Use gzeof() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

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