0

我在使用 HTML 4.01 Strict 的地方有以下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>

<?php echo "Hello World!"; ?> <br>
<?php echo "Hello World!"; ?> <br>

</body>
</html>

但是,HTML 4.01 标准不允许<br>在标签中使用标签。<body>那么,我必须在段落标签中包含我的 PHP 代码吗?

4

2 回答 2

1

"Valid" is a very broad term.

  • Syntactically correct -- yes, the code will run without error
  • Semantically correct PHP -- PHP does not really have a concept of semantics other than best practices. Some argue for and others against combining PHP code with HTML at all, so some would consider what you are doing to be wrong for separate reasons.
  • Semantically correct HTML -- You should consider the HTML that is rendered by PHP as the HTML that is required to be semantically correct. That should be validated and not the input PHP code/HTML combination. I'm not that familiar with the 4.01 standard, so if you need to follow it and it says not to use <br> in <body> (sounds like a bizarre rule to me, though) then just make sure that the output code uses <p> tags correctly. Seems like this would require surrounding the php blocks with <p></p> in this specific case.
于 2013-10-03T13:12:35.423 回答
0

但是,<br>HTML 4.01 标准不允许在标签中使用标签。

在 HTML 4.01 Strict 的 DTD ( http://www.w3.org/TR/html401/strict.dtd ) 中,BR 元素被归类为“特殊”,因此被归类为“内联”。但是 BODY 元素的内容模型不允许“内联”内容,只有“块”元素(加上其他一些:SCRIPT、INS、DEL)。

因此,对于您的示例 PHP 代码生成的 HTML,不仅<br>是无效的标签,还有Hello World!内联内容。

那么,我必须在段落标签中包含我的 PHP 代码吗?

好吧,就 DTD 有效性而言,它不必标签<P>,任何“块级”元素都可以。但是P可能是最好的。(或者你可以考虑 PRE。)

于 2013-10-04T18:05:54.233 回答