Use gzuncompress() when you need to decompresses strings stored in the zlib compressed-data format in a PHP application. The examples below show the actual operation and its limitations.
What Is gzuncompress() in PHP?
Decompresses strings stored in the zlib compressed-data format. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
gzuncompress(string $data, int $max_length = ...): 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
echo gzuncompress(gzcompress("Hello"));
Output:
Hello
Example 2: Different Input or Behavior
<?php
echo gzuncompress(gzcompress("Config"));
Output:
Config
Practical Example: Applying the Function
<?php
$content = "Backup metadata"; $packed = gzcompress($content); $unpacked = gzuncompress($packed); echo $unpacked === $content ? "Verified" : "Mismatch";
Output:
Verified
Important Behavior and Edge Cases
Untrusted compressed input can expand dramatically; validate limits before decompression.
Comparison With Related Functions
| Approach | Difference |
|---|---|
gzuncompress() | Decompresses strings stored in the zlib compressed-data format. |
| Related operation | gzinflate() expects raw DEFLATE, while gzuncompress() expects zlib-wrapped data. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Untrusted compressed input can expand dramatically; validate limits before decompression. |
| Confusing this function with a related operation | gzinflate() expects raw DEFLATE, while gzuncompress() expects zlib-wrapped data. |
Key Takeaways
- Decompresses strings stored in the zlib compressed-data format.
- Untrusted compressed input can expand dramatically; validate limits before decompression.
- gzinflate() expects raw DEFLATE, while gzuncompress() expects zlib-wrapped data.
Use gzuncompress() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.