我需要在带有 html 的 php 文件中使用 !DOCTYPE 声明吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14613030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:38:14  来源:igfitidea点击:

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

phphtmldoctype

提问by DanielFox

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 文件,其中包含我的网站内容。该文件需要是 .php,因为我先获取一些变量,然后在网站内容中使用它。像这个例子:

<?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?

我需要<!DOCTYPE HTML>, 因为文件扩展名是 php 吗?此外,它是否正确放置?它应该出现在标签之前还是文件的第一行?

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

我还注意到,如果我删除了<!DOCTYPE HTML>,我的一些 css 代码将停止工作......

Thank you very much.

非常感谢。

采纳答案by Graham Walters

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

是的,您需要一个 DOCTYPE,因为您的浏览器只能看到上述代码的以下部分

<!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>

我通常像这样将关闭的 PHP 标签和 DOCTYPE 放在一起 ?><!DOCTYPE HTML>

回答by mTorres

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

很抱歉让死者复活,但似乎没有人解释为什么您需要 HTML 中的 doctype(是的,输出 HTML 的 PHP 脚本最后是一个 HTML 文件)。

Declaring a doctype affects the way the browser interprets your HTML (this is probably whyyour 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>).

声明 doctype 会影​​响浏览器解释您的 HTML 的方式(这可能您的 css 代码可能在没有 doctype 的情况下停止工作的原因)。基本上有两种方式:怪癖模式和严格模式。后者坚持标准,因此您应该始终告诉浏览器您的代码遵循哪个 HTML 标准(现在您可能需要具有最简单 doctype: 的 HTML5 <!DOCTYPE html>)。

See here for a more detailed explanation.

有关更详细的说明,请参见此处

回答by Derrick Tucker

As others have said, !DOCTYPEis 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.

正如其他人所说,!DOCTYPE在输出 HTML 的 php 脚本中是必要的。如果您正在创建图像或执行 bash 文件或其他内容,则情况会有所不同。

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.

至于它属于哪里,最好把它放在开头,这样你就不会不小心在它之前输出一些东西,但是如果你正在使用会话变量或发送标头,你需要确保在之前做这些事情声明一个文档类型。请记住,在通过 php 发送标头或启动会话之前,不能有浏览器输出(甚至是空格)。

回答by wajiw

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

无论 html 是如何构建的,您都应该始终在 html 中放入 Doctype。

w3 doctype list

w3 文档类型列表

回答by GordonM

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.

HTML 的相同规则同样适用,无论它们是纯 HTML 文件,还是嵌入了 PHP 的 HTML 文件。在任何情况下,您都需要一个文档类型,您需要一个纯 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)

顺便说一下,在第一个块的末尾 oh PHP 代码和文档类型之间有空格。我似乎记得 doctype 之前的空格可能有问题(尽管这可能仅适用于 XHTML)

回答by Oded

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

是的,您应该有DOCTYPE声明,因为您发送给浏览器的是 HTML,而不是 PHP。

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

该声明告诉浏览器您正在使用 HTML 5 并告诉它如何呈现它。

回答by Jules

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

当它们包含嵌入的 HTML 时,最好将.php文件视为服务器已指示扫描动态 (PHP) 代码的常规 HTML 文件。

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

要使!DOCTYPE文档具有语义,仍然需要有效的声明。

回答by John Conde

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.

您的最终输出是使用文档类型的 HTML。所以,是的,你需要一个。如果它破坏了你的 CSS,那么你的 HTML 和/或 CSS 是错误的,需要修复。

回答by Diodeus - James MacFarlane

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.

PHP 真的与 HTML 无关——它只是服务器端代码吐出一堆浏览器解释的文本。

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

文档类型是浏览器如何解释随后的 HTML 的关键部分。

回答by Thoppil

<!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.

如果你<!DOCTYPE HTML>在 php 文件中放入一些样式将无法正常工作,一些对齐问题。