← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Trailing Whitespace

style Beginner

Also Known As

trailing spaces whitespace cleanup EOL whitespace

TL;DR

Spaces or tabs at the end of a line — invisible but pollutes diffs and can cause subtle PHP issues.

Explanation

Trailing whitespace adds noise to git diffs (lines appear changed when only whitespace changed), can interfere with "headers already sent" errors in PHP if a file ends with whitespace after the closing ?> tag, and makes grep results harder to read. Most editors have a "trim trailing whitespace on save" setting. The PSR-12 standard explicitly prohibits trailing whitespace.

Common Misconception

Trailing whitespace is cosmetic and does not affect code or diffs. Trailing whitespace creates noisy diffs, causes heredoc syntax errors in some PHP versions, and triggers CI linting failures — configure editors to strip it on save and add a pre-commit hook.

Why It Matters

Trailing whitespace creates noise in diffs and can cause subtle bugs in languages where whitespace is significant — eliminating it keeps version history clean and diffs focused on real changes.

Common Mistakes

  • Not configuring editors to trim trailing whitespace on save.
  • Not adding trailing_whitespace = true to .editorconfig.
  • Mixed files where some lines have trailing whitespace and others don't — inconsistent.
  • Trailing whitespace in heredoc strings that alters the string content.

Code Examples

✗ Vulnerable
function greet(string $name): string {
    return 'Hello ' . $name;   
    //                        ^^^ trailing spaces — invisible but in git diff
}

// .editorconfig fix:
[*]
trim_trailing_whitespace = true
✓ Fixed
// Trailing whitespace causes noise in diffs and can break heredoc/string comparisons

// .editorconfig
[*.php]
trim_trailing_whitespace = true

// Auto-fix with phpcbf:
$ vendor/bin/phpcbf --standard=PSR12 src/

// Git pre-commit hook to block commits with trailing whitespace:
$ git config core.whitespace trailing-space

// PhpStorm: Settings → Editor → General → Strip trailing spaces on save
// VS Code: "files.trimTrailingWhitespace": true  (in settings.json)

Added 15 Mar 2026
Edited 22 Mar 2026
Views 23
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S
No pings yesterday
Amazonbot 8 Ahrefs 4 Perplexity 4 Google 1
crawler 16 crawler_json 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Configure your editor to auto-strip trailing whitespace on save and add trim_trailing_whitespace = true to .editorconfig — trailing whitespace causes noisy diffs and can break heredoc strings
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
git diff showing trailing whitespace changes; whitespace-only lines in PHP files; heredoc broken by trailing space after closing marker
Auto-detectable: ✓ Yes php-cs-fixer phpcs editorconfig git
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: File

✓ schema.org compliant