在不刷新的情况下将变量传递给其他PHP

在不刷新的情况下将变量传递给其他PHP,php,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<

我是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>-->
    <?php

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

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

学习PHP
学习PHP
你的PHP学习库


我甚至在functions.php中创建了一个函数,这样我就可以在index.php中键入“get_header()”,而无需再次编写整个代码,从而进一步简化了工作

functions.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>-->
    <?php

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

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

现在,我如何允许这个index.php具有自定义页面标题,而不是header.php提供的默认页面标题

我错过了什么重要的事情吗。我曾尝试创建一个变量并将其传递给functions.php,但没有成功。或者有没有更干净的方法

我被wordpress如何组织他们的文件所启发,我检查了wordpress文件。然后我决定从头开始尝试一些东西,以便更好地理解和提高我的PHP技能

我知道可以使用POST和GET,但我不想仅仅为了更改页面标题而刷新或加载新页面,尤其是index.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>-->
    <?php

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

?>
<?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(); ?>
编辑:

这里我包括了我的index.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>-->
    <?php

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

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

我们学到了什么
在functions.php中

function get_header(){
    if (file_exists('header.php'))
      require 'header.php';
    else echo "There is an error retrieving a file";   
}
在header.php和应答器标题中调用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>-->

学习PHP
你的PHP学习库


在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(); ?>
<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content

我们学到了什么

看起来您需要的只是简单的包含。实际上,在这里使用函数会使它变得更加困难,因为include的作用域与包含它的位置相同。例如

海德尔公司

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

包含的文件可以访问函数作用域中的变量。

很抱歉,我不太明白这是如何工作的。此外,header.php也由index.php加载。index.php>header.php>functions.phpNo如果没有,它会抛出一个错误require(header.php?title=title):无法打开流。我想我遗漏了什么,但我确实把参数放在了get_标题(“标题”)too@Mohd.Zafranudin,结果是什么?是的,它是有效的,这是我想要实现的逻辑,通过查看代码,我了解了很多。非常感谢。session_start()现在对我来说很有意义这不是session变量的用途。它们用于在单独的页面加载之间持久化变量,而不是在单个页面加载中。使用ajax在不刷新页面的情况下传递变量我知道你的意思,但这发生在页面加载之前。不需要AJAX,除非我要求用户单击某个东西,这是不需要的,因为我尝试从服务器端自动执行此操作。