我正在建设一个小型的个人网站。为了使事情变得更小,并避免一遍又一遍地写东西,我有一个header.php文件,每个子目录index.php文件都是include的。因此,每个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>中什么也没有显示。我只想把标题内容放在标题标签中。我怎样才能解决这个问题?
提前谢谢。
发布于 2018-04-22 03:51:30
对不起,麻烦了,我发现了问题!我使用passthru来获取变量的内容,但没有实现屏幕上的打印。是我的错!
编辑:网站说我两天内不能接受这个答案,但这是被接受的。
发布于 2018-04-22 03:34:53
确保您是从web服务器访问页面(例如,XAMPP -然后通过http://localhost/site/index.php访问)。如果您试图直接从您的文件系统打开它们,PHP就会如您所描述的那样简单地显示出来。还要确保index.php有关闭体和html标记。
https://stackoverflow.com/questions/49962205
复制相似问题