在PHP中,在计算包含的文件中的变量的值之前修改它的值


in PHP, Alter the value of a variable from an included file before the value is computed

我目前有一个header.php页面,其中存储了我的标题和布局的所有样式和位置。然后我有一个contactus.php页面,其中包括header.php页面。

-----CONTACTUS.PHP:
<?php 
    include 'header.php' 
    $classnamehere='"linkStyleTwo"'     //Here is where I want to update the value
?>

我的页面顶部的主要链接使用特定的样式:

------HEADER.PHP:
<?php 
    $classnamehere= '"linkStyleOne"' ?>
    <a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......

我想改变这个人点击的链接的样式,这样它就会弹出,告诉这个人他们在哪个页面上。目前,当我尝试改变contactus.php内部的$classnamehere的值(通过简单地给它分配一个新值)来改变<a href>标记内打印的类时,什么也没有发生。它执行include命令,并输出header.php中声明的值,忽略我在新页面上更改值的尝试。

是否有任何方法可以改变contactus.php内的值,同时仍然保持header.php内的初始值,以便所有其他页面仍然可以使用"默认"样式?

编辑:在contactus.php内,我可以改变从header.php获得(包括)的变量的值,而不实际"改变"全局变量吗?

查看您的代码:

   -----CONTACTUS.PHP:
    <?php 
        include 'header.php' 
        $classnamehere='"linkStyleTwo"'     //Here is where I want to update the value
    ?>
The main links at the top of my page are styled in a certain way using:
    ------HEADER.PHP:
    <?php 
        $classnamehere= '"linkStyleOne"' ?>
        <a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......

你的代码做以下事情:

    执行
  • contactus.php。
  • 包含
  • header.php,将这里的$classname设置为'"linkStyleOne"',并使用classname创建实际链接linkStyleOne。
  • 之后$classnamehere被设置为linkStyleTwo

这意味着你必须分配classname BEFORE,包括header.php。

你可以在header.php:

<?php
if ($currentPage) == 'contact') { 
    //Set this class when user is on a specific page
    $classnamehere='"linkStyleTwo"';     
}
else {
    $classnamehere= '"linkStyleOne"';
}
?>

在contactus。php

<?php 
$currentPage = 'contact';
include 'header.php' 
?>

change this

 class= <?php echo '"$classnamehere"' ; ?>.

class= "<?php echo $classnamehere ; ?>"

或者这样用

<?php echo '"', $classnamehere ,'"' ; ?>

或者这样(我不确定这是否有效)

<?php echo '"{$classnamehere}"' ; ?>

如果包含代码,则包含代码中的所有变量都是全局的,因此所有更改都反映在所有部分中。在 header。php

$a = 10;
在<<p> strong> contactus.php
include "header.php"
$a = 0;

没有办法改变$a只有内部接触。$a更改后的所有代码将使用新值。您可以在contactus中创建临时变量$a_copy,然后更改此变量:

$a_copy = $a;
....class= <?php echo '"$a_copy"'...

您应该创建一个PHP类并将其包含在header中,或者将其作为header。在页面上调用它,然后为类变量赋值,然后可以回显一个单独的类函数,该函数使用所需的变量值。如果你不熟悉这个过程,我可以提供一个例子。

Header.php简单示例:

<?php
class Template
{
    private $active_nav;
    private __construct()
    {
    }
    public function set_active($page = '') // set page test value
    {
        $this->active_nav = $page;
    }
    public function get_active() // return page test value
    {
        return $this->active_nav;
    }
    public function nav_links() // this could be changed to return an entire header or part or whatever -- change the scope accordingly
    {
        // active link CSS is linSktyleOne
        $current_page = $this->get_active();
        $nav_links  = '<ul>';
        $nav_links .=   '<li><a href="index.php" class="' . ($current_page == 'index' ? "linkSytleOne" : "linkStyleTwo") . '">Index</a></li>';
        $nav_links .=   '<li><a href="contact.php" class="' . ($current_page == 'contact' ? "linkSytleOne" : "linkStyleTwo") . '">Contact</a></li>';
        $nav_links .= '</ul>';
        return $nav_links;
    }
}

contact.php简单示例:

<?php
require_once('includes/header.php');
$template = new Template;
$template->set_active('contact');
// put all your html or other code here
echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well

index.php简单示例:

<?php
require_once('includes/header.php');
$template = new Template;
$template->set_active('index');
// put all your html or other code here
echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well

这些都有帮助吗?您只需要为每页添加一行代码,如果您真的想要更花哨,您可以通过使用$_SERVER['REQUEST_URI']值的子字符串进行条件测试来删除每页上的set函数调用,并将设置值的条件放在类的构造函数中。