简体   繁体   English

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

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

I have a php file with my website content in it. 我有一个包含我网站内容的php文件。 The file needs to be .php because i get some variables first and then use it later in the website content. 该文件需要是.php,因为我首先获得一些变量,然后在网站内容中使用它。 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? 我需要<!DOCTYPE HTML> ,因为文件扩展名是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... 我还注意到,如果我删除<!DOCTYPE HTML> ,我的一些css代码就会停止工作......

Thank you very much. 非常感谢你。

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>

As others have said, !DOCTYPE is necessary in php scripts that are outputting HTML. 正如其他人所说, !DOCTYPE在输出HTML的PHP​​脚本中是必需的。 If you were creating an image or executing a bash file or something, the story would be different. 如果您正在创建图像或执行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. 至于它所属的位置,最好把它放在开头,这样你就不会在它之前意外输出一些东西,但是如果你使用会话变量或发送标题,你会想要确保在那之前完成这些事情。声明doctype。 Remember, there can be NO browser output (even whitespace) before headers are sent through php or sessions are started. 请记住,在通过php发送标头或启动会话之前,可能没有浏览器输出(甚至是空格)。

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). 很抱歉让死者复活,但似乎没有人解释为什么你需要一个HTML文档类型(是的,输出HTML的PHP​​脚本最后是一个HTML文件)。

Declaring a doctype affects the way the browser interprets your HTML (this is probably why your css code may stop working without a doctype). 声明doctype会影响浏览器解释HTML的方式(这可能您的css代码可能在没有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> ). 后者坚持标准,所以你应该总是告诉浏览器你的代码遵循哪种HTML标准(现在你可能想要HTML5哪个有最简单的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. 是的,你应该有DOCTYPE声明,因为你发送浏览器的是HTML,而不是PHP。

The declaration tells the browser that you are using HTML 5 and tells it how to render it. 该声明告诉浏览器您正在使用HTML 5并告诉它如何呈现它。

You should always put a Doctype in your html, no matter how the html is built. 无论html是如何构建的,你都应该在你的html中放置一个Doctype。

w3 doctype list w3 doctype list

The same rules for HTML apply equally, whether they're plain HTML files, or HTML files with embedded PHP. HTML的相同规则同样适用,无论它们是纯HTML文件还是带有嵌入式PHP的HTML文件。 YOu need a doctype in every circumstance you'd need one in plain HTML. 你需要一个doctype,在任何情况下,你需要一个纯HTML。

By the way, there's whitespace between the end of the first block oh PHP code and the doctype. 顺便说一句,在PHP代码的第一个块的末尾和doctype之间有空格。 I seem to remember that whitespace before the doctype can be problematic (though that might only apply to XHTML) 我似乎记得在doctype之前的空格可能有问题(虽然这可能仅适用于XHTML)

Your final output is HTML which is what uses the doctype. 您的最终输出是HTML,它是使用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. 如果它破坏了你的CSS,那么你的HTML和/或CSS是错误的,需要修复。

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. 当它们包含嵌入式HTML时,最好将.php文件视为常规HTML文件,服务器已被指示扫描动态(PHP)代码。

A valid !DOCTYPE declaration is still necessary for the document to be semantic. 文档语义仍然是有效的!DOCTYPE声明。

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. doctype是浏览器如何解释后面的HTML的关键部分。

<!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文件中,一些样式将无法正常工作,一些对齐问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在用于AJAX的PHP文件中是否需要doctype和其他HTML标记? - Do I need doctype and other HTML tag in the PHP file that used for AJAX? php DOMDocument 添加带有 DOCTYPE 声明的标头 - php DOMDocument adds <html> headers with DOCTYPE declaration [HTML / PHP]:是否需要更改.html文件的扩展名 - [HTML/PHP]: Do I need to change the extension of .html file 我需要在包含的php文件中回显html吗 - Do I need to echo html inside included php file 我应该使用<!DOCTYPE html>在每个文件上? - Should i use <!DOCTYPE html> on every file? html / php包含中没有doctype - No doctype in an include with html/php 我正在重新设计HTML 中的一个PHP 网站。是否因为文件扩展名需要设置重定向? - I am redesigning a PHP website in HTML. Do I need to set up redirects because of the file extension? 如果我在 index.php 文件中连接到 mysql 数据库,是否需要在任何带有嵌入式 php 的链接 html 文件中再次执行此操作? - If I connect to a mysql database in my index.php file, do I need to do it again in any linked html files with embedded php? 为什么在doctype上方有PHP代码? - Why do I have PHP code above the doctype? PHP XMLWriter:我需要将哪些参数传递给XMLWriter :: writeDtdEntity来生成如上所述的嵌套实体DTD声明? - PHP XMLWriter: What parameters do I need to pass to XMLWriter::writeDtdEntity to generate a nested entity DTD declaration as described?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM