Do I need a !DOCTYPE declaration in a php file with html?

79,579

Solution 1

Yes you need a DOCTYPE as your browser only sees the following part of the above code

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
Welcome THE USER NAME
</body>
</html>

I usually place the close PHP tag and the DOCTYPE together like so ?><!DOCTYPE HTML>

Solution 2

Sorry to resurrect the dead, but no one seems to explain why you need a doctype in HTML (yes a PHP script outputting HTML is an HTML file at the end).

Declaring a doctype affects the way the browser interprets your HTML (this is probably why your css code may stop working without a doctype). Basically there's 2 ways: quirks mode and strict mode. The latter is sticking to standards, so you should always tell the browser which HTML standard your code is following (nowadays you probably want HTML5 which has the simplest doctype: <!DOCTYPE html>).

See here for a more detailed explanation.

Solution 3

As others have said, !DOCTYPE is necessary in php scripts that are outputting HTML. If you were creating an image or executing a bash file or something, the story would be different.

As for where it belongs, it's a good idea to put it at the start just so you don't accidentally output something before it, but if you are using session variables or sending headers, you will want to make sure to do those things BEFORE declaring a doctype. Remember, there can be NO browser output (even whitespace) before headers are sent through php or sessions are started.

Solution 4

The same rules for HTML apply equally, whether they're plain HTML files, or HTML files with embedded PHP. YOu need a doctype in every circumstance you'd need one in plain HTML.

By the way, there's whitespace between the end of the first block oh PHP code and the doctype. I seem to remember that whitespace before the doctype can be problematic (though that might only apply to XHTML)

Solution 5

You should always put a Doctype in your html, no matter how the html is built.

w3 doctype list

Share:
79,579
DanielFox
Author by

DanielFox

Updated on February 17, 2020

Comments

  • DanielFox
    DanielFox about 4 years

    I have a php file with my website content in it. The file needs to be .php because i get some variables first and then use it later in the website content. Like this example:

    <?php
    $user_name = $_REQUEST['username'];
    ?>
    
    <!DOCTYPE HTML>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Page Title</title>
        <link rel="stylesheet" href="css/style.css" />
    </head>
    <body>
    Welcome <?php echo $username;?>
    </body>
    </html>
    

    Do I need the <!DOCTYPE HTML>, since the file extension is php? Also, is it placed corretly? Should it come before the tag or in the very first line of my file?

    I also noticed that if I remove the <!DOCTYPE HTML>, some of my css code stops working...

    Thank you very much.