In PHP, header_register_callback() registers a function that runs just before PHP sends the response headers. Your callback can still inspect or change headers in that short window.
Only one callback is active at a time. A later call silently replaces the previous callback. The callback receives no arguments, and its return value is ignored.
Syntax
Call header_register_callback() with the values you need. Start with this line.
Registers a tiny callback; returns true on success.
The callback runs after headers are prepared and before body output. Do not print from the callback, or you can break the response. Callbacks are not stacked.
Required. A callable with no parameters. It runs just before headers are sent.
Return Value
Return Value
Description
bool
true on success, false on failure.
PHP Examples
Example 1 — Add a Header From the Callback
Register a callback that queues one more header, then list headers as a web SAPI would show them.
PHP
<?php
header("Content-Type: text/plain");
header_register_callback(function () {
header("X-Ready: yes");
});
// In a real response, output would flush headers and run the callback.
foreach (["Content-Type: text/plain", "X-Ready: yes"] as $line) {
echo $line, PHP_EOL;
}
Output
Content-Type: text/plain
X-Ready: yes
How It Works
Content-Type is set before the callback is registered.
The callback adds X-Ready just before headers would be sent.
The printed lines show the combined header set a web SAPI would expose.
CLI demos often cannot flush real headers, so the listing is illustrative.
Example 2 — Remove a Header at the Last Moment
Queue a temporary header, then remove it inside the registered callback.