将变量传递给其他PHP而不刷新

时间:2016-07-23 08:18:18

标签: php

我是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(); ?>

2 个答案:

答案 0 :(得分:1)

看起来你需要的只是简单的包含。在这里,你实际上通过使用函数使其变得更难,因为包含的范围与包含它的范围相同。 E.g。

header.inc

…
<title><?php echo isset($title) ? $title : 'Untitled';?></title>
…

的index.php

<?php
$title = 'Welcome';
require 'header.inc';
?>
welcome

另一个-page.php文件

<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content

如果你想使用一个函数,请给它参数。

function get_header($title = 'Some default title') {
    …
}

包含的文件可以访问函数范围内的变量。

答案 1 :(得分:0)

在functions.php

function get_header(){
    if (file_exists('header.php'))
      require 'header.php';
    else echo "There is an error retrieving a file";   
}
在header.php中的

,在balise标题中你调用session参数

<!Doctype html>
<html lang="en">

<head>
    <title>
     <?php if(!empty($_SESSION['title-page'])) echo $_SESSION['title-page']; else  'Learn PHP'; ?>
    </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>-->

并在index.php中

<?php 
    session_start(); 
    $_SESSION['title-page'] = 'this is the welcome Page'; 
    require 'functions.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(); ?>

和另一个page.php

<?php 
    session_start(); 
    $_SESSION['title-page'] = 'this is an another Page'; 
    require 'functions.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(); ?>