Forum Moderators: coopster

Message Too Old, No Replies

<!DOCTYPE> placement?

Where does it go?

         

Emperor

10:28 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



Hi guys,

On my regular HTML pages I put the <!DOCTYPE> right at the very top. What should I do for a PHP/HTML combo page? Like this:

// <!DOCTYPE> here?

<?php
...
php stuff
mostly validation (for forms)
...
?>

// or maybe <!DOCTYPE> here?
// does it matter?

<html>
...
the page
...
some PHP, mostly echo()
</html>

In general, does that seem right?

Thanks,
Cyrus

jollymcfats

10:49 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



As far as I know you don't want any blank lines or empty spaces before a DOCTYPE declaration, so I'd say either keep it at the very top, or be sure that there aren't blank lines between the <?php?> and the DOCTYPE:

<?php yada('yada', 'yada');?>
<!DOCTYPE ...>

<html>...

PHP will remove the carriage return between the <?php?> and DOCTYPE on output. It only removes the first carriage return, though. Any more blank lines between the elements will be sent to the browser.

If you're doing redirects, setting cookies or response headers, you'll need to do all that before you output a DOCTYPE header, as in the example above.

coopster

10:50 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Emperor.

It depends on whether or not you are always going to push the information out to the browser. Sometimes you might do some logical processing and decide that you want to redirect the user to a different page. But it's too late, you've already pushed information to output (the browser) if you have the document type declaration at the beginning of your script.

When you are ready to push the information out, then you indeed want the document type declaration to appear before any root element. Although the HTML 4.01 specification [w3.org] doesn't imply it XHTML [w3.org] does.

Emperor

12:17 am on Dec 3, 2004 (gmt 0)

10+ Year Member



Ok guys, thanks, that clears things up. For my PHP/HTML combo pages I *always* push to the browser so I'll put the <!DOCTYPE> at the very top. On my PHP-only pages (all of them redirect) I won't use any <!DOCTYPE> at all.

Take care.