PHP 包括 HTML 文档的 'head' secton,如果


php include 'head' secton of html doc, if

我有很多页面,大多数都有标题部分,可以"独立"。此外,它们中的大多数都被"包含"在"较大"的文档或文章中。所以,在一个,让我们称之为"大页面",我可以有 3 或 4 个包含的页面,每个页面都有自己的头部信息。有没有一种更奇特的方法可以包含带有所有元、样式等的"头.html,但只有一次,这样如果"父级",比如索引.php已经"包含"了"头.html",包含特殊字符.html不会也加载头,但如果我要加载特殊字符.html它自己,它会"包括"头.html????

(exp:索引.php包括导航.html、nav_r.html、页眉(徽标、欢迎等)、页脚.html、body01.html、特殊字符.html等。但是,我想使用特殊字符.html作为带有标题、样式等的独立文档,用于文档格式设置。

所以,某种包括如果...所以头.html只包括一次。

我希望这是相对清楚的。

提前谢谢你,兰迪斯。Landisreed dot com/index.php - head.html

我想你可以使用

include_once 'header.html';

然后,如果之前包含它,则不会再次包含它。

也就是说,您必须在每个文件中包含标题信息,因此您的specialcharacters.html必须使用它以及body01.html。然后,无论哪个include_once第一个 - 有标题.html都会出现。

编辑:

要区分标题或其他信息,您可以在标题中执行以下操作.html:

<title><?=$title;?></title>

然后在您的每个脚本中

$title = 'Whatever';
include_once "header.html";

现在,谁先调用 header,谁将首先设置$title并将其呈现为标头。一旦它呈现为标题,任何其他包含对$title的后续更改将被您的页面忽略。

你试过使用'include_once'吗? http://www.php.net/manual/en/function.include-once.php

例:

include_once "header.php";
根据

PHP的文档,你可以使用include_once()

include_once语句包含并计算指定的文件 在脚本执行期间。这是一种类似于 包含语句,唯一的区别是如果代码 从已包含的文件,将不再包含。 顾名思义,它只会包含一次。

更多信息请点击此处 http://in3.php.net/include_once

如果我

答对了,我认为您需要的是一个header.php的头文件,以包含在所有带有require_once的页面中。诀窍是将您拥有的所有不同类型的头部,例如一个用于头部.html一个用于特殊字符.html,放入由 if 语句分隔的header.php文件中。header.php可能如下所示:

 if ($caller == 'head') { // HTML for head.html}
 elseif($caller != 'head' && $caller ==  'specialcharacters') 
     { // HTML specific to specialcharacters.html  for standalone viewing}

一旦在满足所有条件的情况下编写了此header.php,您需要在每个文件的顶部相应地设置$caller(例如,对于specialcharacters.php第一行代码应$caller = 'specialcharacters';。然后在指定 $caller 后将header.php作为require_once("header.php")包含在所有文件中。编辑您的index.php文件将如下所示:

$caller = 'in_my_index';
require_once('header.php');

您的specialcharacters.php文件将如下所示:

$caller = ($caller != 'in_my_index')?'in_my_specialchars':'in_my_index'; // This is to make sure that when specialcharacters.php is included inside index.php then it should still show index.php title but when loaded standalone, it will show its own title.
require_once('header.php');

现在您的header.php如下所示:

 <html><head>
 <?PHP    if($caller == 'in_my_index') { echo '<title>I am Index Title</title>';} 
          elseif($caller == 'in_my_specialchars') { echo '<title>I am standalone Specialcharacters.php Title</title>';}
 ?>

我希望这应该会给出一个更好的主意。

希望这有帮助!