html/php 的包含中没有文档类型

No doctype in an include with html/php

我有一个 PHP 包含,其中包含 HTML 和 PHP 代码,并且包含在另一个已经具有 <!doctype html> 标记的页面上。 HTML 验证器 (w3.org) 告诉我它在验证 include 时出错,因为我的 include 文件不包含此标记,但由于 include 用于声明 doctype 的页面,是真的有必要在 include 中声明它吗?

示例:

include.php

<div>
<p> Hello  


<?php
echo "world!";
?></p> </div>

index.php

<!doctype html>
<html>
<head></head>
<body>
<?php
include ("include.php");
?>
</body>
</html>

Error: The document type could not be determined, because the document had no correct DOCTYPE declaration. (include.php)

您的问题是您正试图将 include.php 当作 HTML 文档进行验证。不是,它是 HTML 的片段(并且不应该有自己的 Doctype 声明)。

如果您想独立于包含它的页面对其进行验证,请创建一个最小包装 HTML 文档并将其包含在其中,然后验证包装文档。