Forum Moderators: coopster
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
<?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.
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.