简体   繁体   中英

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

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.

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>

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.

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 .

Yes, you should have the DOCTYPE declaration, since what you send the browser is HTML, not PHP.

The declaration tells the browser that you are using HTML 5 and tells it how to render it.

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

w3 doctype list

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)

Your final output is HTML which is what uses the doctype. So, yes, you need one. If it is breaking your CSS then your HTML and/or CSS is wrong and needs to be fixed.

When they contain embedded HTML, it's better to think of .php files as regular HTML files which the server has been instructed to scan for dynamic (PHP) code.

A valid !DOCTYPE declaration is still necessary for the document to be semantic.

PHP really has nothing to do with HTML - it's just the server-side code spitting out a bunch of text that the browser interprets.

A doctype is a critical part of how the browser interprets the HTML that follows.

<!DOCTYPE HTML>
<table  style="position:absolute; bottom:20%; right:40%;" width="150">

if you put <!DOCTYPE HTML> in php file some styles will not work properly, some alignment problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM