繁体   English   中英

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

[英]PHP echo include variable in <head><title>

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

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

在我的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时会看到此内容),而NOTHING则完全显示在<head> 我只想将标题内容放在标题标签中。 我该如何解决这个问题?

提前致谢。

确保您正在从Web服务器(例如XAMPP-然后通过http://localhost/site/index.php访问)访问页面。 如果您尝试直接从文件系统中打开它们,则将按照您的描述简单地显示PHP。 还要确保index.php具有结束正文和html标记。

麻烦您了,我发现了问题! 我正在使用passthru来获取变量的内容,但我没有意识到要在屏幕上打印。 我的错!

编辑:该网站说我两天不能接受此答案,但这是可以接受的

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM