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
| Approach | Difference |
|---|---|
gzputs() | Writes uncompressed bytes into a gzip stream and is an alias of gzwrite(). |
| Related operation | gzwrite() is an alias-equivalent writing operation; gzputs() uses the same functionality. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Write counts refer to uncompressed input bytes; close the handle to finalize gzip data. |
| Confusing this function with a related operation | gzwrite() 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.