0

我想在我的网站上有一个 header.php 文件。我目前有以下内容:

头文件.php

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="<?php if(isset($depth)){echo $depth;};?>css/style.css">

函数.php

function include_layout_template($template="", $depth="")
{
    global $depth;
    include(SITE_ROOT.DS.'public'.DS.'layouts'.DS.$template);
}

索引.php

<?php include_layout_template('header.php', "../"); ?>

但是 $depth 消失了,我什至无法回显 $depth; 它只是空白。如何获取用于 header.php 的深度变量?

4

2 回答 2

2

您必须在函数调用中重命名深度变量

function include_layout_template($template="", $my_depth="")
{
   global $depth;
   //if need $depth = $mydepth
于 2012-05-14T11:38:36.577 回答
0

您的$depth变量正在消失,因为它首先作为参数传递,然后定义为使用全局参数。

我将用一个例子来解释:

$global = "../../"; //the variable outside
function include_layout_template($template="", $depth="")
{
    global $depth; //This will NEVER be the parameter passed to the function
    include(SITE_ROOT.DS.'public'.DS.'layouts'.DS.$template);
}
include_layout_template("header.php", "../");

要解决,只需修改深度本身以外的函数参数。

function include_layout_template($template="", $cDepth="") { }
于 2012-05-14T11:45:10.543 回答