Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with whitespace before doctype

Tags:

html

php

I got some strange problem where i have 2 copies of same website in 2 different folders. In one of those 2 copies has whitespace before doctype declaration so i have problems to work with facebook as document is formated incorrectly.

Before html there is some php calculations, but there is no echo statements or something.

What could be cause for 2 identique websites under same server in different folders, one having this issue?

like image 969
arma Avatar asked Jan 20 '11 15:01

arma


People also ask

What happens if DOCTYPE is not specified?

The absence of the DOCTYPE or its incorrect usage will force the browser to switch to quirks mode. It means that the browser will do its best to layout the page that is considered to be old or created against web standards.

Will HTML5 work if you do not put Idoctype HTML in your .HTML file?

<! DOCTYPE html> // Tells the browser that we are using HTML5. If document type is not mentioned, browser will go to Quirks mode. Quirks mode depends upon the web browser version, If is older version then this will not support HTML5 tags (Example: header tag, footer tag, section tag,...)

Is it mandatory that the doctype declaration is the first line of every HTML5 page?

All HTML documents must start with a <!DOCTYPE> declaration.

What is the correct HTML5 DOCTYPE?

A doctype declaration tells the browser that the page to be rendered is written in HTML. To declare an HTML5 doctype, `<! DOCTYPE html>` is required in the first line of your HTML document. Doctype declaration for HTML5 is not case sensitive and does not require a closing tag.


2 Answers

I'm almost positively certain that you have some trailing whitespace at the end of a PHP include. Check there first.

Example:

<?php
    // header file
    include 'other_file.php';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"><!-- etc.  -->

Then

<?php
    // other_file.php
    // connects to database, or something,
    // but notice the whitespace after the closing tag
?>



... pretend this text isn't here

I added the note at the end to get stackoverflow's markdown editor to show the additional lines after the closing ?> tag. PHP automatically truncates one newline character after the tag in includes, but if you have more than one they will be interpreted as blank space before your doctype declaration.

Easiest fix? Remove the closing tag ?> from all of your includes. This is a valid technique, and could be the fastest way for you to get up and running.

like image 118
Stephen Avatar answered Sep 20 '22 13:09

Stephen


You don't say which browser you're having problems with. IE used to check the first few letters of the page for a doctype and, if it isn't one (such as whitespace), it would go into quirks mode.

like image 41
Rob Avatar answered Sep 23 '22 13:09

Rob