【问题标题】:PHP: How can I reference variables from an included file before it's been included?PHP:如何在包含文件之前从包含的文件中引用变量?
【发布时间】:2011-04-12 23:58:33
【问题描述】:

如何在包含文件被包含之前从包含的文件中引用变量?或者我可以在将其 HTML 插入正文标记之前以某种方式包含该文件(以便稍后引导其变量)吗?或者我可以将家庭的所有正文内容包含在一个大变量中,我也可以在索引中回显吗?

这是我想要做的:

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!

【问题讨论】:

    标签: php variables include


    【解决方案1】:

    只需将包含语句移动到文件顶部即可。 这会将所有值、函数和变量公开给所有后续行。

    <?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>
    

    【讨论】:

    • 好的,那么我是否将 home.php 中的全部主要内容变成一个长变量?
    • 是的。您可以将所有变量放在 HOME.PHP 中,确保将其包含在文件顶部。
    【解决方案2】:

    简答版:你不能。如果您这样做,您将收到“未定义变量”通知。

    我发现拥有一个包含在索引、家庭、联系人或任何其他文件中的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 分离。它将帮助您以及将来要编写代码的任何人。

    希望这会有所帮助!

    【讨论】:

    • 这是一个很好的解决方案,它肯定会成为我的后备方案,但我仍然很好奇是否有一种方法可以让您拥有一个主模板文件,然后包含单独的子页面文件他们自己的标题数据(标题、元标记)以及正文内容。
    • 您可以使用 John Ballinger 的解决方案并将正文内容存储在一个变量中,但是您需要将一个变量发送到索引文件(参见 $_GET ),并在 @987654335 中使用它@ 语句让脚本知道它应该实际包含和显示哪个页面。但是,我不确定所有链接只有一个基本文件(即 index.php?do=home 或 index.php?do=about_us 等)有多健康,以及这对不同的关键字和描述意味着什么,因为我没有不要摆弄 SEO。
    • 请看我更新的答案,我相信它更接近你想要达到的目标。
    【解决方案3】:

    我会看看使用模板系统。将您的代码与内容分离将为您将来节省很多麻烦。它还将允许您在将来轻松更改 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');
    ?> 
    

    这只是模板系统功能的皮毛。您可以重复或可选块,包括其他模板等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多