我试图让整个<head>部分成为自己的包含文件.一个缺点是标题和描述以及关键字是相同的; 我无法弄清楚如何将参数传递给包含文件.
所以这是代码:
<?php include("header.php?header=aaaaaaaaaaaaaaaaaaaaa"); ?>
<body>
.....
..
.
Run Code Online (Sandbox Code Playgroud)
<!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=" <?php $_GET["header"]?> " >
<meta name="Description" content=" <?php $_GET["header"]?> " >
<title> <?php $_GET["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>
Run Code Online (Sandbox Code Playgroud)
显然这不起作用; 如何将参数传递给包含的文件?
Rol*_*olf 95
因为似乎没有人提到它...... Include具有它所调用的行的范围.因此,如果您不想创建新的全局变量,可以在函数内调用include.例:
function includeHeader($title) {
include("inc/header.php");
}
Run Code Online (Sandbox Code Playgroud)
$ title将在包含的文件中定义.如果您希望从包含的文件中看到多个变量,则可以传递数组...让我们创建一个通用函数
function includeFile($file, $variables) {
include($file);
}
Run Code Online (Sandbox Code Playgroud)
瞧!
如果希望包含的代码能够更改传递的变量的值,则可以通过引用传递(使用"&"):
function includeFileWithVariables($fileName, $variables) {
extract($variables);
include($fileName);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果您在包含的文件中更改$ title的值,它的值也将在"主文件"(调用代码)中更改.将此应用于数组,您可能必须这样做:
includeFileWithVariables("header.php", array(
'keywords'=> "Potato, Tomato, Toothpaste",
'title'=> "Hello World"
));
Run Code Online (Sandbox Code Playgroud)
但是我已经走开了,我甚至不确定它是否有效.我认为它应该但我有一个糟糕的经验,使用PHP中的数组内部引用传递,我认为最好避免它.
PS:怎么样(使用提取物):
function includeHeader($title) {
include("inc/header.php");
}
Run Code Online (Sandbox Code Playgroud)
现在你所要做的就是:
function includeFile($file, $variables) {
include($file);
}
Run Code Online (Sandbox Code Playgroud)
这里我们只传递了数组中的一个变量,但可能还有更多.
Mic*_*l M 46
index.php文件:
<?php
$my_header = 'aaaaaaaaaaaaaaaaaaaa';
include 'header.php';
?>
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
这不是一个理想的解决方案,但我知道这是你在PHP中的第一步.
PS.您的Doctype与代码不匹配.我已经将你的标题html调整为XHTML.
Mic*_*zek 12
您不能传递参数include,但它可以访问您已设置的所有变量.从include文档:
包含文件时,它包含的代码将继承发生包含的行的变量范围.从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用.
从而:
<?php
$header = 'aaaaaaaaaaaaaaaaaaaaa';
include("header.php");
?>
Run Code Online (Sandbox Code Playgroud)
<title> <?php echo $header; ?> </title>
Run Code Online (Sandbox Code Playgroud)