PHP echo 包含变量<head><title>



我正在建立一个小型个人网站。为了使事情保持较小并避免一遍又一遍地写东西,我有一个header.php文件,每个子目录index.php文件include'S。因此,每个index.php文件都有以下行:

$title = someTitleVariableOrMethod;
include('/var/testsite/docroot/header.php');

,在我的header.php文件中,我有这些行(我知道我可以改进格式,但首先我想使标题正常工作(。

<html>
<head>
 <?php
   if (isset($title)) {
    echo '<title>' . $title . '</title>';
   } else {
    echo '<title>Sampletext</title>';
   }
 ?>
  <style>
    //a bunch of irrelevant css
  </style>
</head>
<body>
//this is the end of the header file, the rest is dealt with in the index.php file

但是,由于某种原因,标题的内容和我所有的CSS都出现在<body>的开头(当我在浏览器中按F12时,我会看到这一点(,并且在<head>中根本没有出现。我只希望标题内容放在标题标签中。我该如何解决此问题?

预先感谢。

确保您正在从Web服务器访问页面(例如XAMPP-然后通过http://localhost/site/site/index.php访问。如果您尝试直接从文件系统打开它们,则如您所述,PHP将仅显示。还要确保index.php具有关闭主体和HTML标签。

对不起,我发现了问题!我正在使用passthru来获取变量的内容,我没有意识到屏幕上的打印。我的错误!

编辑:该网站说我两天都无法接受这个答案,但这被接受

最新更新