Content · Programming · PHP

PHP gzgetc() Function: Usage and Examples

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

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

Use gzgetc() when you need to reads one uncompressed character from a gzip stream in a PHP application. The examples below show the actual operation and its limitations.

What Is gzgetc() in PHP?

Reads one uncompressed character from a gzip stream. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

gzgetc($stream): string|false

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,"AB"); gzclose($file); $file = gzopen($path,"rb"); echo gzgetc($file); gzclose($file); unlink($path);

Output:

A

Example 2: Different Input or Behavior

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"XY"); gzclose($file); $file = gzopen($path,"rb"); gzgetc($file); echo gzgetc($file); gzclose($file); unlink($path);

Output:

Y

Practical Example: Applying the Function

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"DOG"); gzclose($file); $file = gzopen($path,"rb"); while (($character = gzgetc($file)) !== false) echo $character; gzclose($file); unlink($path);

Output:

DOG

Important Behavior and Edge Cases

Reads bytes, not Unicode grapheme clusters; false signals read failure or exhaustion.

Comparison With Related Functions

ApproachDifference
gzgetc()Reads one uncompressed character from a gzip stream.
Related operationgzread() reads multiple bytes at once; gzgetc() reads one.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationReads bytes, not Unicode grapheme clusters; false signals read failure or exhaustion.
Confusing this function with a related operationgzread() reads multiple bytes at once; gzgetc() reads one.

Key Takeaways

  • Reads one uncompressed character from a gzip stream.
  • Reads bytes, not Unicode grapheme clusters; false signals read failure or exhaustion.
  • gzread() reads multiple bytes at once; gzgetc() reads one.

Use gzgetc() 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.