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
| Approach | Difference |
|---|---|
header_register_callback() | Registers a callback invoked before PHP sends response headers. |
| Related operation | header() sets a header immediately; header_register_callback() schedules work before headers are sent. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Callback timing is just before headers are sent; do not perform slow or recursive output operations there. |
| Confusing this function with a related operation | header() 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.