Content · Programming · PHP

PHP header_register_callback() Function: Usage and Examples

Understand PHP header_register_callback() with three examples, return behavior, important pitfalls, and related operations.

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

Use header_register_callback() when you need to registers a callback invoked before PHP sends response headers in a PHP application. The examples below show the actual operation and its limitations.

What Is header_register_callback() in PHP?

Registers a callback invoked before PHP sends response headers. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

header_register_callback(callable $callback): bool

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

if (PHP_SAPI === "cli") { echo "Requires HTTP runtime"; } else { header_register_callback(function () { header("X-App-Stage: ready"); }); }

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

if (PHP_SAPI !== "cli" && !headers_sent()) { header_register_callback(fn() => header("X-Demo: active")); } else { echo "Browser request required"; }

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

$callback = function () { header("Cache-Control: private, no-store"); }; if (PHP_SAPI !== "cli" && !headers_sent()) { header_register_callback($callback); } else { echo "HTTP headers unavailable here"; }

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

Callback timing is just before headers are sent; do not perform slow or recursive output operations there.

Comparison With Related Functions

ApproachDifference
header_register_callback()Registers a callback invoked before PHP sends response headers.
Related operationheader() sets a header immediately; header_register_callback() schedules work before headers are sent.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationCallback timing is just before headers are sent; do not perform slow or recursive output operations there.
Confusing this function with a related operationheader() sets a header immediately; header_register_callback() schedules work before headers are sent.

Key Takeaways

  • Registers a callback invoked before PHP sends response headers.
  • Callback timing is just before headers are sent; do not perform slow or recursive output operations there.
  • header() sets a header immediately; header_register_callback() schedules work before headers are sent.

Use header_register_callback() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

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