I am no guru, but I can generally hold my own in HTML. A !doctype is pretty important to use in HTML and it seems the same situation should apply to HTML within a PHP script.

Howerver, I often see in PHP code snippets simply:

<html>
<body>
...
</body>
</html>

but no !doctype.

  1. Without a !doctype how does a server know what type of HTML to serve? Or does it not matter somehow?

  2. Or perhaps is the doctype only needed once the first time it's used?

I've looked at the Manual and at W3schools plus Google without finding it mentioned.

TIA,

Rivet

    Rivet;11028873 wrote:

    A !doctype is pretty important to use in HTML and it seems the same situation should apply to HTML within a PHP script.

    First, note that there's no difference between the two, namely because the latter doesn't really exist. Whether your webserver is relaying the contents of a .html file verbatim OR the PHP scripting engine is executing a .php script that outputs data, the end result is the same - the webserver sends some data to the user's browser (which then interprets that data accordingly, e.g. as HTML markup).

    Rivet;11028873 wrote:

    1. Without a !doctype how does a server know what type of HTML to serve? Or does it not matter somehow?

    As far as your webserver is concerned, no, it doesn't matter. The webserver's job is simply to relay data from your server to any client requesting it; it couldn't care less whether or not that data contains a "doctype" or anything resembling valid HTML for that matter.

    Rivet;11028873 wrote:

    2. Or perhaps is the doctype only needed once the first time it's used?

    If you're talking about how browsers interpret and render a webpage, no, it is needed every time.

    Rivet;11028873 wrote:

    I've looked at the Manual and at W3schools plus Google without finding it mentioned.

    Which manual? If you're referring to the PHP manual, again, note that PHP couldn't care less whether or not you output any data that contains a "doctype" or anything resembling valid HTML for that matter.

    As for W3schools... don't bother using it for any sort of official reference at all, lest you become a W3fool. 😉

      It depends on the browser how it will handle a page without a doctype declaration. Nowadays I think most will default to the HTML5 doctype, but this probably varies depending on version and platform.

        [QUOTE=Rivet;11028873]I often see in PHP code snippets simply:

        <html>
        <body>
        ...
        </body>
        </html>

        but no !doctype.

        [/QUOTE]
        This is probably just laziness for the most part. I put up snippets with no doctype all the time here when typing out sample code here on phpbuilder. I've also noticed that for simple examples it is usually enough. Declaring a doctype is probably not critical when you are using a trivial HTML example like a DIV or paragraph or something. If you get into CSS or Javascriptor layout stuff, it matters a lot more.

        Rivet;11028873 wrote:

        I1. Without a !doctype how does a server know what type of HTML to serve? Or does it not matter somehow?

        Keep in mind that the doctype is more for the benefit of the client receiving the data than it is for the server. The guy who programmed the server might be super careful to use HTML5 for all his HTML and utf-8 encoding for all his text, etc, etc, etc so the server may know EXACTLY what it is serving. Without a doctype, a browser or other client that receives the server's data might have no idea how to interpret it.

        Rivet;11028873 wrote:

        I2. Or perhaps is the doctype only needed once the first time it's used?

        No you have to send a doctype with each document you send. You might send HTML5 for some pages and an older version for other pages -- sites can be quite large and sprawling and it's not uncommon to have new sections and old sections.[/QUOTE]

          bradgrafelman;11028875 wrote:

          Which manual? If you're referring to the PHP manual, again, note that PHP couldn't care less whether or not you output any data that contains a "doctype" or anything resembling valid HTML for that matter.

          As for W3schools... don't bother using it for any sort of official reference at all, lest you become a W3fool. 😉

          Informative, easy to read; Thanks!!

          Yes, I meant the PHP Manual; I use it quite bit. Google usually puts W3Schools as the next item found, and I've found that convenient because sometime their explanations and sample codes are easier to understand; an adjunct to the Manual if you will.
          I HAVE wondered occasionally aboit W3Schools though; a lot of times the data is old and doesn't even mention things later than PHP4 and has led me astray tht way a couple times.
          The w3fool link was interesting, to say the least. I think I agree with and have experienced being mislead on occasion, but I do wish some of the more garish comments were accompanied by links or citations to back up some of the more problematic allegations.
          I do however think the site has clarified/verified a few of my wonderings; verification is always an ego boost of sorts<g>.

          Thanks much!

            Not sure I understand what you mean. The specs for major browsers are easily available but in the end it's not a client-side issue, but a server-side issue I asked about.

              Okay, but the doctype is purely for the client's benefit; it doesn't matter to the server. bradgrafelman's second paragraph.

                Rivet;11028893 wrote:

                ... Google usually puts W3Schools as the next item found

                Doesn't have to.

                Rivet;11028895 wrote:

                Not sure I understand what you mean. ...in the end it's not a client-side issue, but a server-side issue I asked about.

                It's no more, or less, a server-side issue than any of your output is. The point is, PHP doesn't care. If you care (and I agree that you should), then change your code a bit to make sure it gets output.

                  🙂 Wow, those are pretty good links. That'll keep me reading for a bit!

                  Thanks.

                    Write a Reply...