HTML标题信息出现在正文中

时间:2017-02-23 15:38:40

标签: php html header

我创建了一个简单的网页,其中包含页眉和页脚作为单独的php文件,如下所示

<?php
$PageName = "Home Page";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";

include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>

这是标题

<?php
print("<!DOCTYPE html>");
print("<html lang='en-UK'>");
print("<head>");
print("<title>");
print($PageName);
print("");
print("</title>");
print("<meta http-equiv='content-type' content='text/html; charset=utf-8' >");
print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
$CSSRoot = "/MyPage/StyleDefault.css";
print("<link rel='stylesheet' type='text/css' href=$CSSRoot>");
print("</head>");
print("<body>");
print("<h1>My Page</h1>");?>

和页脚

<?php print("</body></html>");?>

但是当我查看它时,标题元素出现在正文中,如下所示

header information appearing in the body

我想说清楚这不会导致任何问题,但我想知道原因是什么。

由于

修改

大脑放屁时刻将代码放在评论中,抱歉。

新索引

    <?php
    $PageName = "Home Page";
    $CSSRoot = "/MyPage/StyleDefault.css";
    include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
    <h1>My Page</h1>
<?php   
    include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>

新标题

    <!DOCTYPE html>
<html lang="en-UK">
<head>
   <title><?php echo $PageName;?></title>
   <meta http-equiv='content-type' content='text/html; charset=utf-8'>
   <meta name='viewport' content='width=device-width, initial-scale=1.0'>
   <link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>

新页脚

</body></html>

新输出

<html lang="en-UK"><head></head><body>


   <title>Home Page</title>
   <meta http-equiv="content-type" content="text/html; charset=utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" type="text/css" href="/MyPage/StyleDefault.css">

    <h1>My Page</h1>
</body></html>

2 个答案:

答案 0 :(得分:0)

这就是你应该怎么做的。

<强>的header.php

<!DOCTYPE html>
<html lang="en-UK">
<head>
   <title><?php echo $PageName;?></title>
   <meta http-equiv='content-type' content='text/html; charset=utf-8'>
   <meta name='viewport' content='width=device-width, initial-scale=1.0'>
   <link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>

<强>的index.php

<?php

$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>

<h1>My Page</h1>

<?php include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>

<强> footer.php

</body>
</html>

这应该可行,我不知道你为什么要使用php打印所有的html标签,而你可以输出正常的。

答案 1 :(得分:0)

我相信我发现了问题,chrome中的sources面板显示,在doctype和end body标签之前是两个字符(即页眉和页脚文件的开头),由红点表示,根据{{3这是一个零宽度的无空间,我不知道它们是如何出现的,我似乎无法摆脱它们,但似乎它们可能是问题的原因。 / p>

更新

浏览了这个问题,使用UFT-8 BOM(字节顺序标记)插入违规字符,将其更改为UTF-8(在Notepadd ++中编码)的php文件解决了问题,谢谢所有的帮助

相关问题