2011-08-13 40 views
0

我有一个header.php和一个footer.php文件。我的HTML标头位于header.php,我有一个index.php文件。如何将数据发送到以前包含的PHP文件?

我使用这样(的index.php):

require 'header.php'; 

$example_code = 'example'; 

︙ 

require 'footer.php'; 

而且我的header.php

<html> 
<head> 
    <title> 
    ??? 
    <title> 
    <meta name="description" content="???" /> 
    <meta name="keywords" content="???" /> 
</head> 
<body> 
︙ 

我想从index.php文件发送一些数据header.php在那里打印它(请参阅???)。我正在考虑header()函数,但我在PHP手册中看不到任何示例。

+0

你能澄清你想达到什么吗? –

回答

3

你能做的最好的事情是从表现分离的逻辑。使用MVC方法,您可以在其中关注一个文件中的所有逻辑,然后显示您在仅用于展示的图层中所做的结果。

除此之外,如果你想保持你的方法,你只需要在header.php包含之前作出分配。因此,假设你想改变你的网页的标题,这是你需要做的:

的index.php

<?php 
$title = 'My Page Title'; 
$description = 'My meta description'; 
$keywords = 'keyword list'; 
include('header.php'); 

?> 

的header.php

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

就这么简单。只要记住你不能指定一个页面/脚本,已包括后,这样的

虽然,我试图回答你,不一定建议这种方法。如果你的应用程序只有几页,没关系。如果它更大(或将要),像MVC模式(两步查看模式)是一个更好的替代恕我直言。

+0

非常好的技术:) –

+0

很高兴你发现这样的技术有用。从演示中分离逻辑可以帮助您特别在维护您的应用程序时。下一次使用标题词时要小心谨慎 - 特别是如果再加上header()函数,它适用于另一种标题),因为它可能是误解的根源。 :-) – maraspin

+1

htmlspecialchars .... http://php.net/manual/en/function.htmlspecialchars.php – hakre

1
<?php 

$tpTitle="Helping you to improve your web site"; 

    $pgHeading="Site-Report.com - Helping you to improve your web site"; 

    $pgDesc="Helping you to improve your web site"; 

$pgKeywords="site-report"; 

    ?> 


<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> 

<title><?php echo $tpTitle ?></title> 

<meta name="description" content="<?php echo $pgDesc ?>"></meta> 

<meta name="keywords" content="<?php echo $pgKeywords ?>"></meta> 

</head> 



http://www.cre8asiteforums.com/forums/index.php?showtopic=4558 
+0

我说我知道这个功能,但我怎样才能这个代码的功能?我可以看到示例代码吗? –

1

php头函数与html标签“head”无关。

+1

你不明白我的问题。 –

1

header()函数不适合你想要做的事情。你只是在寻找一个变量:

的index.php:

$title = 'My Page Title!'; 
$description = 'This is how I describe it.'; 
$keywords = 'page, title, describe'; 

的header.php:

<title> 
     <?php echo htmlspecialchars($title); ?> 
    <title> 
    <meta name="description" content="<?php echo htmlspecialchars($description); ?>" /> 
    <meta name="keywords" content="<?php echo htmlspecialchars($keywords); ?>" /> 
相关问题