PHP:如何在包含文件包含之前引用变量?

时间:2010-09-16 02:02:02

标签: php variables include

如何在包含文件被包含之前引用它?或者我可以以某种方式包含该文件(所以我可以稍后引导它的变量),然后将HTML直接插入到body标签中?或者我可以在一个大变量中包含所有家庭的身体内容,我也可以在索引中回复它?

这是我正在尝试做的事情:

index.php

<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>

<?php include 'home.php'; ?>

</body>
</html>


home.php

<?php
$title="home page";
$description="this is the home page"; 
$keywords="home, awesome, yes";
?> 

this is the home page content that gets inserted into the body!

3 个答案:

答案 0 :(得分:0)

只需将include语句移到文件顶部即可。 这会将所有值,函数和变量暴露给所有后续行。

<?php include 'home.php'; ?>
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>



</body>
</html>

答案 1 :(得分:0)

简答题版:你不能。如果你这样做,你会得到一个'未定义的变量'。

我发现通常更方便的是header.php(和footer.php),它包含在索引,主页,联系人或其他任何文件中。优点是您没有冗余代码,如果需要在页眉或页脚中进行修改,则只需要修改一个文件。

例如,'about_us.php'看起来像:

<?php
include('path/to/header.php');
#body goes here
include('path/to/footer.php');
?>

你的标题就像是:

<?php
$title = ucfirst(str_replace('_', ' ', substr(basename($_SERVER['PHP_SELF']), 0, -4));
?>
<html>
    <head>
        <title><?php echo $title; ?> page</title>
        <meta name="description" content="this is the home page" />
        <meta name="keywords" content="home, awesome, yes" />
    </head>
    <body>

$title变量将是文件名,减去扩展名,所有下划线都用空格替换,第一个单词的首字母大写。所以基本上about_us.php将被转换为“关于我们”。这是必然是一般解决方案,但我作为示例给出了它,请记住您想在原始示例中使用动态标题。对于动态描述和关键字,基于文件名,您还可以借助switch()语句指定不同的值。


<强>更新

另一个解决方案,虽然与你所要求的相反,但同时更接近你正在寻找的是header.php喜欢

<html>
    <head>
        <title><?php echo $title; ?> page</title>
        <meta name="description" content="<?php echo $desc; ?>" />
        <meta name="keywords" content="<?php echo $keywords; ?>" />
    </head>
    <body>

...页脚就像......

    </body>
</html>

...然后将它们包含在您的其他文件中:

<?php
$title = 'Your title';
$desc = 'Your description';
$keywords = 'The, big, brown, fox, jumps, over, the, lazy, dog';
include('path/to/header.php');
?>

<!-- body goes here -->

<?php
include('path/to/footer.php');
?>

这样,您可以在包含引用它们的文件之前分配所有变量,所有链接都有不同的文件,并且您不需要花哨的开关。另外作为旁注,用PHP包装正文的HTML只是不好的做法。尝试尽可能地将HTML与PHP分开。它将帮助您和未来将要处理代码的任何人。

希望这有帮助!

答案 2 :(得分:0)

我想看一下使用模板系统。将代码与内容分开将为您节省很多麻烦。它还允许您以后轻松更改html模板。另外,您无需运行php代码即可查看模板。

看一下smarty模板 http://www.smarty.net/

然后你会建立一个模板文件:“template.tpl”

<html>
  <head>
    <title>{$title}</title>
    <meta name="description" content="{$description}" />
    <meta name="keywords" content="{$keywords}"/>
  </head>
  <body>
    {$home_content}
  </body>
</html>

和一些运行的PHP代码:

<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->assign('title'        , 'Your title');
$smarty->assign('description'  , 'Your description');
$smarty->assign('keywords'     , 'The, big, brown, fox, jumps, over, the, lazy, dog');
$smarty->assign('home_content' , 'this is the home page content that gets inserted into');
$smarty->display('template.tpl');
?> 

这只是触及模板系统可以做的事情的表面。你可以重复或可选的块,包括其他模板等。