Php 如何将参数传递到包含的文件?

Php 如何将参数传递到包含的文件?,php,include,Php,Include,我正试图使整个部分成为自己的include文件。一个缺点是标题和描述与关键字相同;我不知道如何将参数传递给include文件 下面是代码: index.php ..... .. . 页眉 marc,当您使用include时,您只需设置一个变量即可使用: <?php $var = "Testing"; include("header.php"); ?> 在头文件中: <?php echo $var; ?> 允许您以前定义的变量在任何包含中都可用

我正试图使整个
部分成为自己的include文件。一个缺点是标题和描述与关键字相同;我不知道如何将参数传递给include文件

下面是代码:

index.php

.....
..
.
页眉


marc,当您使用include时,您只需设置一个变量即可使用:

<?php
  $var = "Testing";
  include("header.php");
?>

在头文件中:

<?php
  echo $var;
?>


允许您以前定义的变量在任何包含中都可用。

您考虑得太多了

<?php 
$header = "aaaaaaaaaaaaaaaaa";
include("header.php"); 
?>

::编辑::

我决定增加价值

包含的文件将获得包含它的位置的范围。因此,如果在函数中包含一个文件:

<?php
$get_me = "yes";
function haha()
{
include("file.php");
}
haha();

// And file.php looks like

echo $get_me; // notice + blank

?>
function includeHeader($title) {
    include("inc/header.php");
}
function includeFile($file, $variables) {
    include($file);
}

此外,您多次包含同一文件,效果非常好

<?php

$output = "this";
include("cool_box.php");

$output = "will";
include("cool_box.php");

$output = "work";
include("cool_box.php");

?>

甚至可以使用它来加载成为类中方法一部分的模板。因此,您可以执行以下操作:

<?php

class template
{

    private $name;

    function __construct($name)
    {
        $this->name = preg_replace("/[^a-zA-Z0-9]/", "", $name);
    }

    function output(array $vars)
    {
        include($this->name.".php"); // Where $vars is an expected array of possible data
    }

}

$head = new template("header");
$body = new template("body");
$head->output();
$head->output(array("content" => "this is a cool page"));

?>

您不能将参数传递给
include
,但它可以访问您已设置的所有变量。从:

包含文件时,它包含的代码将继承包含文件所在行的变量范围。从该点开始,调用文件中该行可用的任何变量都将在被调用文件中可用

因此:

index.php

页眉
如果包含一个文件,就像将该代码插入父文件一样。您可以简单地执行以下操作:

<?php
$parameter = "Hello World";
include("header.php");
?>
我使用了
isset()
方法来验证它是否已经有值并且已经实例化。

index.php:

<?php
$my_header = 'aaaaaaaaaaaaaaaaaaaa';
include 'header.php';
?>

和header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php echo $my_header ?> " />
<meta name="Description" content=" <?php echo $my_header ?> " />
<title> <?php echo $my_header ?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>


按照许多人的建议,在
include()
之前将变量定义为伪参数/变通方法是个坏主意。它在全局范围内引入了一个变量。在包含的文件中定义一个函数,以捕获要传递的参数。

这是一个很好的方法。然而,我会做一点由内而外。为网页定义布局和包装,并将内容文件包含在其中:

layout.phtml

<html>
    <head>
      ... your headers go here
    </head>
    <body>
      <? include $content ?>
    </body>
</html>

这样,您可以很好地分离业务逻辑和表示。它还可以帮助您为页面的某些部分(如徽标或导航)剪切重复代码,这些部分在整个网站上重复。

Include具有调用它的行的范围

如果不想创建新的全局变量,可以使用函数包装
include()

<?php
$get_me = "yes";
function haha()
{
include("file.php");
}
haha();

// And file.php looks like

echo $get_me; // notice + blank

?>
function includeHeader($title) {
    include("inc/header.php");
}
function includeFile($file, $variables) {
    include($file);
}
无论何时使用值调用
includeHeader
,都会在包含的代码中定义
$title
,例如
includeHeader(“我喜欢的标题”)

如果要传递多个变量,则始终可以传递数组而不是字符串

让我们创建一个通用函数:

<?php
$get_me = "yes";
function haha()
{
include("file.php");
}
haha();

// And file.php looks like

echo $get_me; // notice + blank

?>
function includeHeader($title) {
    include("inc/header.php");
}
function includeFile($file, $variables) {
    include($file);
}

使用可使其更加整洁:

function includeFileWithVariables($fileName, $variables) {
   extract($variables);
   include($fileName);
}
现在您可以执行以下操作:

includeFileWithVariables("header.php", array(
    'keywords'=> "Potato, Tomato, Toothpaste",
    'title'=> "Hello World"
));

知道它将导致变量
$keywords
$title
在包含的代码范围内定义。

我注意到没有人建议使用模板引擎。我来这里是因为对于我正在处理的项目,模板引擎是不可能的,这可能也是您的情况,但是我认为值得一提的是:(我首选的引擎)并且两者都允许将特定变量传递给include

我强烈建议尽可能使用模板引擎,因为它简化了前端代码,在前端和后端之间添加了一层抽象,Twig和Smarty会自动清除传递给它们的变量,这有助于减轻XSS攻击

树枝示例

header.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="{{ header }}" >
<meta name="Description" content="{{ header }}" >
<title> {{ header }} </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>

{{header}}
index.html

{% include 'header.html' with { 'header' : '<script>alert("this shouldnt work")</script>'} only %}
Body Text
{% include 'footer.html' %}
{%include'header.html'和{'header':'alert(“此不应工作”)}仅%}
正文
{%include'footer.html%}
智能示例

header.tpl

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="{$header}" >
<meta name="Description" content="{$header}" >
<title> {$header} </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>

{$header}
index.tpl

{include 'header.tpl' header='<script>alert("this shouldnt work")</script>'}
Body Text
{include 'footer.tpl'}
{include'header.tpl'header='alert(“这不应该起作用”)}
正文
{包括'footer.tpl'}

是的,但我认为你现在对他来说太超前了。我认为他的方法值得探索,然后才能找到更合适的方法来组合网页。嗯,等等,你怎么做?再做一步,输出包含的文件,然后返回字符串,你就可以构建一个完整的模板引擎。再往前走一步,你就拥有了90%的框架。我希望能做得更好,而且这种方法看起来非常好。。。至少当函数完成时,代码是诗意的!PHP非常适合编写富有诗意的代码。这也是伟大的写作诗歌,没有人能理解。双刃剑。@DampeS8N-re:
双刃剑
-这就是联机文档的用途-这就解释了你的基本问题。但您应该继续寻找更好的方法,如何将html和php组合在一起。@JosefSábl例如,MVC框架。上面示例的问题是,包含的文件不知道变量$my_头来自何处,这让人头痛不已。有一个这样的变量或非常简单的网页是可以的,但依赖这些技术的全尺寸应用程序的维护将很快成为一场噩梦。相信我,我必须维护一个:-)这种方法看起来不错,但意味着对我的代码进行了太多修改。。。我们将继续其他建议…@marc:好的,但是下次你开始计划一个新项目时,考虑一些在这篇文章中分享的建议,并将你的应用程序框架设计得更好一些。评论中可能出现重复的内容?这个答案是对这个问题投票最多的一个,并且有一些评论(大部分是赞扬!),现在都不见了!拥有足够权限的版主必须已删除这些内容。也许你可以在历史记录中进行检查。也要明确这一点:即使那些变量会出现
{include 'header.tpl' header='<script>alert("this shouldnt work")</script>'}
Body Text
{include 'footer.tpl'}