Use gzdeflate() when you need to compresses a string into raw DEFLATE bytes in a PHP application. The examples below show the actual operation and its limitations.
What Is gzdeflate() in PHP?
Compresses a string into raw DEFLATE bytes. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
gzdeflate(string $data, int $level = ..., int $encoding = ...): 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
$compressed = gzdeflate("Hello"); echo gzinflate($compressed);
Output:
Hello
Example 2: Different Input or Behavior
<?php
$compressed = gzdeflate(str_repeat("B",60)); echo strlen($compressed);
Output:
6
Practical Example: Applying the Function
<?php
$payload = "Cache entry data"; $packed = gzdeflate($payload,6); echo gzinflate($packed);
Output:
Cache entry data
Important Behavior and Edge Cases
Use gzinflate() to decode raw DEFLATE; gzuncompress() expects zlib-format data.
Comparison With Related Functions
| Approach | Difference |
|---|---|
gzdeflate() | Compresses a string into raw DEFLATE bytes. |
| Related operation | gzcompress() includes zlib wrapping, unlike gzdeflate(). |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Use gzinflate() to decode raw DEFLATE; gzuncompress() expects zlib-format data. |
| Confusing this function with a related operation | gzcompress() includes zlib wrapping, unlike gzdeflate(). |
Key Takeaways
- Compresses a string into raw DEFLATE bytes.
- Use gzinflate() to decode raw DEFLATE; gzuncompress() expects zlib-format data.
- gzcompress() includes zlib wrapping, unlike gzdeflate().
Use gzdeflate() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.