Content · Programming · PHP

PHP deflate_add() Function: Usage and Examples

Learn how PHP deflate_add works with practical examples, important behavior, relevant alternatives and common mistakes.

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

Compresses the next chunk of data in a streaming compression context.

What Is deflate_add() in PHP?

Compresses the next chunk of data in a streaming compression context.

Its syntax is:

deflate_add(DeflateContext $context, string $data, int $flush_mode = ZLIB_SYNC_FLUSH): string|false

Parameters:

  • $context — Accepted as DeflateContext; required argument.
  • $data — Accepted as string; required argument.
  • $flush_mode — Accepted as int; optional default ZLIB_SYNC_FLUSH.

Return type: string|false. The flush mode determines whether compression has completed. Do not append more data after ZLIB_FINISH.

Availability: Requires zlib.

Example 1: Basic Usage

<?php

$compressionContext = deflate_init(ZLIB_ENCODING_RAW); echo strlen(deflate_add($compressionContext, "hello", ZLIB_FINISH));

Output:

7

The flush mode determines whether compression has completed. Do not append more data after ZLIB_FINISH.

Example 2: A Different Input

<?php

$compressionContext = deflate_init(ZLIB_ENCODING_GZIP); echo strlen(deflate_add($compressionContext, "report", ZLIB_FINISH));

Output:

26

The flush mode determines whether compression has completed. Do not append more data after ZLIB_FINISH.

Practical Example: Applying It in a Program

<?php

$compressionContext = deflate_init(ZLIB_ENCODING_RAW); $compressed = deflate_add($compressionContext, "one ", ZLIB_NO_FLUSH); $compressed .= deflate_add($compressionContext, "two", ZLIB_FINISH); echo inflate_init(ZLIB_ENCODING_RAW) ? strlen($compressed) : 0;

Output:

9

The flush mode determines whether compression has completed. Do not append more data after ZLIB_FINISH.

Important Behavior and Edge Cases

  • Key behavior: The flush mode determines whether compression has completed. Do not append more data after ZLIB_FINISH.
  • Environment: Requires zlib.

Comparison With Related Functions

FunctionPurpose
deflate_add()Compresses the next chunk of data in a streaming compression context.
deflate_init()deflate_init() sets up a compression context; deflate_add() sends data through that context.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: The flush mode determines whether compression has completedThe flush mode determines whether compression has completed. Do not append more data after ZLIB_FINISH.

Key Takeaways

  • Compresses the next chunk of data in a streaming compression context.
  • The flush mode determines whether compression has completed. Do not append more data after ZLIB_FINISH.
  • Requires zlib.

Summary: The flush mode determines whether compression has completed. Do not append more data after ZLIB_FINISH.

Share this resourceSend this page to someone who may find it useful.