PHP error guide
Output buffering does not fix header ordering: meaning and fix
Error summary
Correct response ordering first; use output buffering only when buffering is an intentional response design, with balanced lifecycle calls.
What it means
Correct response ordering first; use output buffering only when buffering is an intentional response design, with balanced lifecycle calls.
What the error means
A header call occurs after output was flushed. Buffering can delay transmission, but it does not make late header mutation a sound application structure.
Why PHP produces it
Content is emitted before status, cookies, redirects, or other headers are finalized, or an output buffer was flushed earlier than expected.
PHP version notes
The behavior described for Output buffering does not fix header ordering applies to PHP 7.4–8.4 unless a narrower version is stated; exact wording can vary by SAPI and patch release.
Most common causes
- ob_flush or flush commits data before header calls.
- Nested buffers are closed in the wrong order.
- A template emits content before controller redirect logic.
Minimal examples
BAD — reproduces the problem
echo $body; header("Location: /done");
FIXED — safer pattern
header("Location: /done", true, 302); exit;
Step-by-step diagnosis
- Inspect ob_get_level and ob_list_handlers before the failing header call.
- Use headers_sent to identify the first committed output.
- Trace ob_start, ob_end_flush, flush, and implicit_flush calls.
Fixes
Finalize headers before body generation
Set status and headers first, then render; buffer only when response capture is required.
header("Content-Type: text/html; charset=UTF-8");
ob_start();
renderPage();
$body = ob_get_clean();
echo $body;
Common mistakes when fixing it
- Setting output_buffering globally as a substitute for ordering.
- Calling ob_clean without understanding nested buffers.
- Continuing execution after a redirect header.
How to prevent it
- Use a response object or front controller.
- Keep templates free of redirect and cookie logic.
- Test headers and body separately.
Web server / environment notes
fpm, apache. Web-server and proxy buffering do not change PHP’s logical header state.
Tags: fpm,apache