Content · Programming · PHP

PHP gzputs() Function: Usage and Examples

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

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

Use gzputs() when you need to writes uncompressed bytes into a gzip stream and is an alias of gzwrite() in a PHP application. The examples below show the actual operation and its limitations.

What Is gzputs() in PHP?

Writes uncompressed bytes into a gzip stream and is an alias of gzwrite(). The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

gzputs($stream, string $data, ?int $length = ...): int|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"); gzputs($file,"Hello"); gzclose($file); echo "Saved"; unlink($path);

Output:

Saved

Example 2: Different Input or Behavior

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); echo gzputs($file,"ABCDE",3); gzclose($file); unlink($path);

Output:

3

Practical Example: Applying the Function

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); foreach (["one","two"] as $line) gzputs($file,$line."
"); gzclose($file); $file = gzopen($path,"rb"); echo gzread($file,50); gzclose($file); unlink($path);

Output:

one
two

Important Behavior and Edge Cases

Write counts refer to uncompressed input bytes; close the handle to finalize gzip data.

Comparison With Related Functions

ApproachDifference
gzputs()Writes uncompressed bytes into a gzip stream and is an alias of gzwrite().
Related operationgzwrite() is an alias-equivalent writing operation; gzputs() uses the same functionality.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationWrite counts refer to uncompressed input bytes; close the handle to finalize gzip data.
Confusing this function with a related operationgzwrite() is an alias-equivalent writing operation; gzputs() uses the same functionality.

Key Takeaways

  • Writes uncompressed bytes into a gzip stream and is an alias of gzwrite().
  • Write counts refer to uncompressed input bytes; close the handle to finalize gzip data.
  • gzwrite() is an alias-equivalent writing operation; gzputs() uses the same functionality.

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