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
| Approach | Difference |
|---|---|
gzeof() | Tests whether a gzip stream has reached end of file. |
| Related operation | feof() applies to ordinary streams; gzeof() applies to gzip resources. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Check read failures separately from EOF; do not loop forever on an empty read. |
| Confusing this function with a related operation | feof() 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.