【发布时间】:2016-11-27 03:20:22
【问题描述】:
我是 PHP 新手。我有这 3 个文件:
- index.php
- functions.php(用于组织函数)
- header.php
我想简化(到目前为止已经完成)index.php 页面,因此我不需要一次又一次地编写所有内容。所以我创建了 header.php 可以被 index.php 加载:
header.php
<!Doctype html>
<html lang="en">
<head>
<title>Learn PHP</title> <!--This is the problem, every page that loads header.php will have same title! -->
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
我什至通过在 functions.php 中创建一个函数来进一步简化事情,这样我就可以在 index.php 中键入“get_header()”而无需编写再次完整的代码。
functions.php
<?php
function get_header(){
if (file_exists('header.php')){
require 'header.php';
}
else{
echo "There is an error retrieving a file";
}
}
?>
现在,我如何允许这个 index.php 有自定义页面标题,而不是 header.php 给出的默认值?
我是否错过了一些重要的事情。我尝试创建一个变量并尝试将其传递给functions.php,但它不起作用。或者有什么更清洁的方法吗?
wordpress 如何组织他们的文件让我受到启发,我检查了 wordpress 文件。然后我决定从头开始尝试一些东西,以便我更好地理解并提高我的 PHP 技能。
我知道可以使用 POST 和 GET,但不,我不想刷新或加载新页面只是为了更改页面标题,尤其是 index.php
编辑:
这里我包含了我的 index.php
<?php
require 'functions.php';
?>
<?php
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li><a href="sample">Syntax</a> </li>
<li><a href="sample">Variables</a> </li>
<li><a href="sample">Code Flow</a> </li>
<li><a href="sample">Arrays</a> </li>
<li><a href="sample">Superglobals</a> </li>
</ul>
</table>
<?php get_footer(); ?>
【问题讨论】:
-
使用ajax传递变量而不刷新页面
-
我知道你的意思,但这发生在页面加载之前。不需要 AJAX,除非我要求用户点击某些东西,这是不需要的,因为我尝试从服务器端自动执行此操作。
标签: php