Use inflate_add() when you need to decompresses the next chunk of a compressed stream using an inflate context in a PHP application. The examples below show the actual operation and its limitations.
What Is inflate_add() in PHP?
Decompresses the next chunk of a compressed stream using an inflate context. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
inflate_add(InflateContext $context, string $data, int $flush_mode = ...): 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
$packed = gzdeflate("Hello"); $context = inflate_init(ZLIB_ENCODING_RAW); echo inflate_add($context,$packed,ZLIB_FINISH);
Output:
Hello
Example 2: Different Input or Behavior
<?php
$packed = gzcompress("Config"); $context = inflate_init(ZLIB_ENCODING_DEFLATE); echo inflate_add($context,$packed,ZLIB_FINISH);
Output:
Config
Practical Example: Applying the Function
<?php
$original = "streamed payload"; $compressed = gzdeflate($original); $context = inflate_init(ZLIB_ENCODING_RAW); $restored = inflate_add($context,$compressed,ZLIB_FINISH); echo $restored === $original ? "Restored" : "Mismatch";
Output:
Restored
Important Behavior and Edge Cases
Encoding format must match; untrusted compressed data can expand dramatically.
Comparison With Related Functions
| Approach | Difference |
|---|---|
inflate_add() | Decompresses the next chunk of a compressed stream using an inflate context. |
| Related operation | gzinflate() decompresses a complete raw DEFLATE string; inflate_add() handles chunks. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Encoding format must match; untrusted compressed data can expand dramatically. |
| Confusing this function with a related operation | gzinflate() decompresses a complete raw DEFLATE string; inflate_add() handles chunks. |
Key Takeaways
- Decompresses the next chunk of a compressed stream using an inflate context.
- Encoding format must match; untrusted compressed data can expand dramatically.
- gzinflate() decompresses a complete raw DEFLATE string; inflate_add() handles chunks.
Use inflate_add() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.