简体   繁体   English

PHP:是否可以修改包含的php文件的变量?

[英]PHP: Is it possible to modify varibles of an included php file?

I am new to php. 我是php新手。 I have a header.php with variables which I want to be uniquely set depending on which php page includes the header.php. 我有一个header.php,它的变量要根据哪个php页面包含header.php进行唯一设置。 I want each page that loads the header to display unique data in the header - current page info. 我希望每个加载标题的页面在标题中显示唯一数据-当前页面信息。
Currently I have something like the following: 目前,我有如下内容:

header.php header.php

     <?php $pageInfo = "";?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" type="text/css" href="css/header.css">
        <title></title>
    </head>
    <body>
        <div id="header-box">
            <div id="header-wrapper">
                <div id="logo-box">
                </div>
                <div id="info-box">
                    <div>
                        <p><?php echo $pageInfo ?></p>                      
                   </div>
                </div>
        </div>
    </body>
 </html>

page1.php page1.php

<body>
    <div><?php $pageInfo = ":D"; require 'shared/header.php'; ?></div>
    <div><?php include 'shared/menu.php';?></div>
</body>

Yes that is possible. 是的,那是可能的。 Include works like as you have have merged multiple pages (Scripts) into one. 包括作品,就像您已将多个页面(脚本)合并为一个页面一样。 So if you want to change the value of variables in header.php before including header.php define them. 因此,如果要在包含header.php之前更改header.php中变量的值,请定义它们。

ex: 例如:

<?php 
$pageInfo  = 'Test page';
require_once 'header.php';
?>

Note: Do not declare $pageInfo = '' in header.php else it will over write it. 注意:不要在header.php中声明$ pageInfo =”,否则它将覆盖它。 You can do as follow in header.php: 您可以在header.php中执行以下操作:

if(!isset($pageInfo)) {
 $pageInfo = '';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM