• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

PHP and DOCTYPE

I notice that when PHP includes HTML it never includes the DOCTYPE line. It always starts right off with <html><head>.  I'm just wondering why the DOCTYPE isn't necessary.

Thanks.
0
steva
Asked:
steva
  • 6
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
DOCTYPE is necessary at many levels.  Just use PHP to generate the correct one for your situation.
0
 
Beverley PortlockCommented:
" I'm just wondering why the DOCTYPE isn't necessary."
 
I'm wondering exactly what srt of includes you are doing. Still......

DOCTYPE is necessary and if it is missing then one is assumed. I would expect that the reason it is dropped in an inclusion is so that you can have a DOCTYPE that is the lowest common denominator for the included HTML

Another point to consider is that there should only be one DOCTYPE per HTML document. If the the included HTML all had DOCTYPEs then you would have multiple DOCTYPEs and heave only know what a browser would make of it.

Remember that IE, in particular, uses the DOCTYPE to render in 'quirks' or 'strict' mode.
0
 
stevaAuthor Commented:
bportlock,

I'm not including anything yet.  I'm just reading.  In Valade's PHP & MySQL for Dummies" , for example, there's an example on pp282-283 where a complete html file ("form_upload.inc") , without DOCTYPE, is included if the form hasn't been submitted yet.  The included file displays the form :

if(!isset($_POST['Upload'}))
{   include(form_upload.inc");
}

From what you're saying, though, form_upload.inc really should have a DOCTYPE at the top. I understand what you're saying about not wanting  multiple includes to include multiple DOCTYPE lines, but that's not the case here.  There's only the one include.


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Beverley PortlockCommented:
I would expect to put my own DOCTYPE at the top of any code I write whether or not I have any includes in the code. It sounds like what you have to me is a error in the book or the matching code. include or include_once will not remove a DOCTYPE if one is in the code, but I would expect most developers never to add a DOCTYPE to code that is being included as part of a page.

If the included document is a whole page from <DOCTYPE.... to </html> then that would be fair enough, but for a fragment like a form I would astonished if it had a DOCTYPE in it.

0
 
Beverley PortlockCommented:
I meant to reply specifically to this comment, but I forgot to add it

"From what you're saying, though, form_upload.inc really should have a DOCTYPE at the top. "

If the included document is a whole page from <DOCTYPE.... to </html> then that would be fair enough, but for a fragment like a form I would astonished if it had a DOCTYPE in it.

"a complete html file ("form_upload.inc") , without DOCTYPE, is included if the form hasn't been submitted yet."

I would expect that this code would be embedded in more code that that the surrounding code would set the DOCTYPE like so


<DOCTYPE ......>
<html>
...... more stuff

if(!isset($_POST['Upload'}))
{   include(form_upload.inc");
}

..... more stuff
</html>

so I would not want the included file to supply a DOCTYPE
0
 
stevaAuthor Commented:
Ok, I think I got it.  One way or another there needs to be a DOCTYPE at the top of every page handed down to the browser.  

Thanks
0
 
Ray PaseurCommented:
Ok, I think I got it.  One way or another there needs to be a DOCTYPE at the top...

Right.

DOCTYPE is necessary at many levels.  Just use PHP to generate the correct one for your situation.
0
 
Beverley PortlockCommented:
"One way or another there needs to be a DOCTYPE at the top of every page handed down to the browser.  "

Yes - that is correct. If you don't supply a DOCTYPE then the browser makes its own decision and that may not be what you want. This is particularly true of Internet Explorer where the absence of a DOCTYPE allows the browser to slip into "Microsoft legacy mode". Read this http://en.wikipedia.org/wiki/Quirks_mode and in particular read this bit http://en.wikipedia.org/wiki/Quirks_mode#Triggering_different_rendering_modes

IE6/7/8 all switch back to the rendering for IE 5.5 in quirks mode. To see what the difference are scroll about halfway down this page http://www.quirksmode.org/css/quirksmode.html

Cheers!
0
 
Ray PaseurCommented:
Sigh.  All my life I have been tilting at windmills, trying to get software developers to create conventional programming constructs and useful error messages.  As you can see, I failed miserably in PHP; just look at the text of any parse error.  So what do we get with HTML and CSS?  The most broadly used "programming" languages, ever, ... and they simply malfunction in silence when you code it wrong.

That's why things like the W3C validator are so useful.  If you're not sure what your programming will do when all the different browsers get hold of the code, you at least have some hope of getting it right once you have validated everything.

Quirksmode is a great site!
0
 
Beverley PortlockCommented:
LOL! Sorry Ray.... have an extra coffee and look at this website using IE (because you cannot see the screw-ups in FF) http://www.positioniseverything.net/ie-primer.html 

@OP - Always use a DOCTYPE that forces the browser in to some form of "Standards Mode" such as HTML 4.01 / XHTML Transistional / XHTML Strict. Any of them are way, way better than quirks mode. And don't stand in front of any windmills if Ray is about.....

;-)

0
 
Beverley PortlockCommented:
@Ray - I actually quite like the "silent failure" of HTML / CSS / PHP. I look at some ASP sites disclosing all sorts of information when they fail and I am so glad that PHP doesn't do that. Having said that it does have its irritating moments when you realise that error_reporting is OFF when you thought it was ON.....

Cheers

Brian
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now