关于php include 文件后,根据需求修改变量的问题

有这些文件
nav.php (公共部分)
a.php
b.php
c.php
d.php
e.php

nav.php 中代码

<?php
$tf = "false";
echo 
'<dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf . ' }"></dt></dl>
<dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf . ' }"></dt></dl>
<dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf . ' }"></dt></dl>
<dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf . ' }"></dt></dl>
<dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf . ' }"></dt></dl>'
?>

a.php,b.php,c.php,d.php,e.php 都引入nav.php

<?php include '../nav.php'; ?>

现在我希望打开 a.php 的时候,上面第一个 <dt></dt> 中的 $tf = "true"; 其他为$tf = "false";
同理,打开b.php的时候,第二个 <dt></dt> 中的 $tf = "true"; 其他为$tf = "false";

求解,谢谢!

阅读 2.7k
1 个回答

nav.php

<?php
unset($tf);
global $tf;
$tf = array_pad(array(), 5, 'false');

function common($tf) {
    echo 
    '<dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf[0] . ' }"></dt></dl>
    <dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf[1] . ' }"></dt></dl>
    <dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf[2] . ' }"></dt></dl>
    <dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf[3] . ' }"></dt></dl>
    <dl><dt class="qp-ui" data-qp-ui="{ \'SideNavZippy\':' . $tf[4] . ' }"></dt></dl>';
}
?>

a.php

include '../nav.php';
global $tf;
$tf[0] = 'true';
common($tf);

b.php

include '../nav.php';
global $tf;
$tf[1] = 'true';
common($tf);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题