Question

I've been struggling with this problem for a long time now, but I cannot really find the solution. The problem is that < !DOCTYPE html etc... does not start at the first line, but leaves four blank lines before it starts.

All my files (header.php, index.php etc) have no line breaks before they start.

Anyone with any similar problems/experiences out there? It would have been of huge help!

See here for reference: view-source:http://2famous.tv/

Thank you

Was it helpful?

Solution

This is most often not caused by leading but by trailing whitespace. Lots of old PHP code still closes down code at the end, which then all too often has a stray newline:

<?php

  // Lot of source code

?>   <----- and a newline here which is the culprit!

To avoid this issue, never close files with ?> - PHP doesn't need it and will just stop parsing at EOF, thus implicitly avoid this 'garbage' in the output.

As for finding the files causing it - good luck, I'd start with combing any custom extensions for this and just removing all ?> markers that you can find.

As an alternative, you can probably 'fix' it by adding a single ob_start() call to your index.php, and then in the template containing the doctype executing ob_end_clean() - this puts all intermediate output in the output buffers, and then trashes it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top