2012-05-14 43 views
0

我希望在我的网站中有一个header.php文件。目前,我有以下几点:在函数中包含的文件中使用变量

的header.php

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

的functions.php

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

的index.php

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

但是$深度dissappears,我甚至不能echo $ depth;它只是空白。我怎样才能得到在header.php中使用的深度变量?

+2

$深度既是全局变量又是函数'include_layout_template'的参数?那一定是搞砸了...... – Yaniro

回答

2

你必须深入变量函数调用

function include_layout_template($template="", $my_depth="") 
{ 
    global $depth; 
    //if need $depth = $mydepth 
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="") { } 
相关问题