UnderHost
Knowledgebase Docs

Headers already sent error-diagnosis and fix

Fix "headers already sent" PHP error. Remove whitespace, BOM, or redirect code before output.

On this page

The "Headers already sent" error means PHP tried to send HTTP headers after output was already sent to the browser. Headers must be sent before any output (HTML, whitespace, or text).

UTF-8 BOM is the most common cause

Many text editors (especially on Windows) save PHP files with a UTF-8 BOM (Byte Order Mark). The BOM is invisible but counts as output, causing the error.

To fix:

  1. Open the affected file in your editor (usually wp-config.php, functions.php, or a custom plugin)
  2. If using VS Code: File → Save with Encoding → UTF-8 without BOM
  3. If using Notepad++: Encoding → Encode in UTF-8 without BOM
  4. Save and reload your site

Extra whitespace before or after PHP tags

If a PHP file has whitespace, blank lines, or HTML after ?>, it will cause this error:

<?php
function my_function() {
  // code
}
?>

<!-- This blank line is output! Remove it -->

Fix: Remove any content after the final ?>. It's actually best practice to omit the closing ?> tag entirely in files that are pure PHP:

<?php
function my_function() {
  // code
}
// No closing ?> tag needed

Checklist to fix headers already sent

  1. Open the file mentioned in the error message
  2. Save it as UTF-8 without BOM
  3. Remove any whitespace before <?php or after ?>
  4. Check for any HTML outside of PHP comments
  5. Check for any echo, print, or output before header() or session_start()
  6. Reload your site and test
Error message shows file and line

The error message includes the filename and line number where the output occurred. Start debugging there.

Related: Parse errors | Output whitespace issues

Was this article helpful?

Still troubleshooting?

Use UnderHost tools for quick checks, or open a support ticket when the issue needs account or server access.

Related articles

Back to Troubleshooting