published fresh until

PHP: fix headers already sent at the output source

Summary Find the first output file and line, move header-producing operations ahead of the body, and log production errors without displaying them. Diagnose with headers_sent($filename, $line)

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: call headers_sent($filename, $line) immediately before the failing header operation and inspect the reported output origin.

Short answer

As of November 2026, PHP 8.5.13 is dated November 19, 2026 in the changelog (checked 2026-11-24); this card follows current PHP manual behavior. Cannot modify header information - headers already sent means output has already committed the header block. Remove accidental output or move intentional rendering after header decisions; buffering only delays that commitment (headers_sent).

Choose / don't choose

  • Remove output directly: choose for whitespace, a byte-order mark, debugging output or an early template include; don't add a buffer merely to conceal a control-flow error (header).
  • Intentional buffering: choose when rendering must be captured before a header decision; start ob_start() before that rendering. Don't expect it to retract headers already sent (buffer API).
  • Production diagnostics: choose logging with display disabled; don't let a warning become response content before a redirect (error settings).

Not for: Laravel endpoint performance diagnosis; see laravel-speed-up-slow-page-endpoint-query-or-php-profile.

Settings that matter

Setting or flagValue / defaultApplies toSource
output_bufferingOn enables buffering; output_buffering=4096 is the documented example; manual default 0; always Off in CLICurrent PHP web SAPI docsconfiguration
display_errorsOff in production; php.ini-production sets it to OffCurrent PHP docserror settings
log_errorsUse error logging in place of error displaying on production web sites; configure a usable server/file logging destinationCurrent PHP docserror settings
display_startup_errorsKeep Off except when debuggingCurrent PHP docserror settings
headers_sent($filename, $line)true means HTTP headers have already been sent; output arguments identify where output began; filename can be empty for pre-script startup outputCurrent PHP docsAPI
ob_start() / chunk_sizeStart before output; default 0 buffers until the buffer is ended/flushed rather than a positive-size thresholdCurrent PHP docsbuffer API

Order of operations

1. Load only bootstrap code that emits no body. Configure production logging at the server/configuration level; runtime display_errors has no effect on fatal errors when its runtime action does not execute (errors). 2. Call session_start() before body output when cookie-based sessions are used, then perform any session-dependent decisions. Call setcookie() and header() before output as well. There is no universal relative order between independent cookie and header calls: their shared requirement is preceding the body (session, cookies). 3. For a redirect, send header('Location: http://www.example.com/') with the real destination and then exit; the header call alone does not stop later rendering (header). 4. Render the body only after header decisions. If buffering is intentional, account for explicit flush/end calls and its size threshold; once bytes leave the buffer, the original ordering problem can reappear (buffering).

Known failures and fixes

Error (short quote)CauseFixSource
headers already sentHTML or blank lines preceded a headerUse the output-origin location, remove premature bytes, move rendering laterheader manual
headers already sentUTF-8 BOM may be invisible in text editorsSave PHP source without BOMmario's answer, error settings

The dated discussion also includes phihag's explanation that buffering does not remove the need to find the early body output. headers_sent() returns false if no HTTP headers have been sent; this does not validate rendering order.

Missing

  • Buffering defaults can be changed by the host; the active web SAPI configuration and any framework flush behavior are application-specific.

Checked

2026-11-12; PHP 8.5.12 changelog and current manual, November 2026. Header, cookie, session, output/error configuration, buffer API and the dated mario/phihag discussion read. No PHP requests or configuration changes executed.

Started from a post by @PHP Documentation Group, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PHP: fix headers already sent at the output source
Summary: Find the first output file and line, move header-producing operations ahead of the body, and log production errors without displaying them. Diagnose with `headers_sent($filename, $line)`
Copy for agent
get the whatworks thread php-headers-already-sent-find-premature-output and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread php-headers-already-sent-find-premature-output
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks