简体   繁体   English

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

[英]PHP: How can I reference variables from an included file before it's been included?

How can I reference variables from an included file before it's been included? 如何在包含文件之前引用包含文件中的变量? Or can I somehow include the file (so I can lead its variables later) before its HTML is literally inserted into the body tag? 还是可以在HTML实际插入body标记之前以某种方式包含文件(以便稍后可以引导其变量)? Or can I contain all of home's body content in one big variable that I can echo as well in the index? 还是可以将一个房间的全部身体内容包含在一个我可以在索引中也可以回显的大变量中?

Here's what I'm trying to do: 这是我想做的事情:

index.php 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 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!

Just move the include statement to the top of the file. 只需将include语句移到文件顶部即可。 This will expose all values, functions and variables to all subsequent lines. 这会将所有值,函数和变量显示给所有后续行。

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

Short answer version: You can't. 简短答案版本:您不能。 You'll get an 'Undefined variable' notice if you do that. 如果这样做,您将收到“未定义的变量”通知。

I find it is usually much more convenient to have a header.php (and a footer.php for that matter) which gets included in the index, home, contact or whatever other file. 我发现通常有一个header.php (与此相关的footer.php )要方便得多,它可以包含在索引,家庭,联系人或任何其他文件中。 The advantage is that you don't have redundant code, and if you need to make a modification in the header or footer, you need to only modify one file. 优点是您没有多余的代码,并且如果需要在页眉或页脚中进行修改,则只需修改一个文件。

So for example, 'about_us.php' would look like: 因此,例如,“ about_us.php”如下所示:

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

And your header would be something like: 您的标头将类似于:

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

The $title variable will be the file name, minus the extension, with all underscores replaced by spaces and the first letter of the first word capitalized. $title变量将是文件名,减去扩展名,所有下划线均用空格替换,首个单词的首字母大写。 So basically about_us.php would be converted into "About us". 因此,基本上about_us.php将被转换为“关于我们”。 This is not necessarily a general solution, but I gave it as an example keeping in mind that you wanted to use a dynamic title in your original example. 不一定是一个通用的解决方案,但我把它作为想到的一个例子保管,你想在你原来的例子使用动态标题。 For dynamic description and keywords, based on the file name you could also assign different values with the help of a switch() statement. 对于动态描述和关键字,还可以基于文件名使用switch()语句分配不同的值。


UPDATE: 更新:

Another solution, although kind of the reverse of what you're asking, but at the same time much closer to what you're looking for would be to write the header.php like 另一个解决方案,虽然有点与您要的相反,但与此同时,更接近您要寻找的是编写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>

... the footer like ... ...像...的页脚

    </body>
</html>

... and then include them in your other files: ...然后将它们包含在您的其他文件中:

<?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');
?>

This way, you are assigning all the variables BEFORE you are including the files in which they are being referenced, you have distinct files for all the links and you don't need fancy switches. 这样,您将在分配所有变量之前(包括要在其中引用它们的文件),为所有链接使用不同的文件并且不需要花哨的开关。 Also as a side note, wrapping the body's HTML in PHP is simply bad practice. 另外要注意的是,将主体的HTML封装在PHP中只是一种不好的做法。 Try to keep the HTML separated from the PHP as much as possible in general. 通常,尽量使HTML与PHP分开。 It will help both you, and whoever is going to do work on the code in the future. 它将为您和将来将要从事此代码工作的人提供帮助。

Hope this helps ! 希望这可以帮助 !

I would have a look at using a template system. 我将看看使用模板系统。 Separating your code from the content will save you a lot of trouble in the future. 将代码与内容分开可以为您节省很多麻烦。 it will also allow you to change the html template easily in the future. 它还将允许您将来轻松更改html模板。 plus you can see your template without having to run the php code. 另外,您无需运行php代码即可查看模板。

have a look at smarty templates http://www.smarty.net/ 看看smarty模板http://www.smarty.net/

you would then build a template file: "template.tpl" 然后,您将构建一个模板文件:“ template.tpl”

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

and some php code to run: 和一些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');
?> 

And that is just scratching the surface of what a templating system can do. 但这仅仅是模板系统可以做的事情的表面。 you can repeating or optional bocks, include other templates, etc etc. 您可以重复或可选的模板,包括其他模板等。

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

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