Whitespace before output-causes and fixes
Fix whitespace output before headers, cache issues, and invisible text in PHP files.
Whitespace output refers to invisible spaces, tabs, newlines, or the UTF-8 BOM before your PHP code runs. PHP treats this as output, which causes "Headers already sent" errors or breaks functionality.
Common causes
- UTF-8 BOM: File saved with BOM by Windows editors (see header-already-sent error)
- Whitespace after ?> at the end of files
- Blank lines before <?php tag
- HTML before PHP code: Content output before headers
- Multiple files included: One file has trailing whitespace that pollutes the output
How whitespace output appears
- "Headers already sent" error when trying to redirect or set cookies
- Extra blank line at the top of the page (visible in View Source)
- JSON output breaks because of extra whitespace before the {
- WordPress installation fails with "headers already sent" message
How to fix whitespace output
Step 1: Check for UTF-8 BOM
This is the most common cause. Open the affected file in your editor:
- VS Code: File → Save with Encoding → UTF-8 without BOM
- Notepad++: Encoding → Encode in UTF-8 without BOM
- Sublime Text: View → Line Endings (ensure it shows UTF-8, not UTF-8 with BOM)
Step 2: Remove trailing whitespace
Check files (especially wp-config.php, functions.php, and custom plugins):
<?php
// Your code here
// Remove any blank lines below this closing tag
?>
Better yet, omit the closing ?> tag if the file contains only PHP:
<?php
// Your code here
// No closing ?> needed
Step 3: Check for HTML before PHP
Ensure no HTML, spaces, or newlines appear before the opening <?php tag:
<?php
// This should be the very first line
// No whitespace above this tag
?>
Prevention
- Always save PHP files as UTF-8 without BOM
- Omit closing ?> tag in pure PHP files (no whitespace after it)
- Use a good code editor (VS Code, Sublime, PhpStorm) that highlights whitespace issues
- Configure your editor to trim trailing whitespace automatically
- Use PHP-FPM strict mode or linting tools to catch these before production
Whitespace is invisible but counts as output. Check View → View Page Source in your browser - if there's a blank line at the very top of the page, whitespace is being output.
Related: Headers already sent | Parse errors
Still troubleshooting?
Use UnderHost tools for quick checks, or open a support ticket when the issue needs account or server access.





















