PHP - 递归函数中的变量范围

时间:2015-09-19 12:34:17

标签: php

第一个假设:假设,我们在一个函数中定义了一个变量(它的名字是$ tmp)(functioin名称是'ExpMenu')用于临时计算,在函数结束时我们返回这个变量。

第二个假设:假设,我们递归地调用该函数以创建基于多维数组的导航菜单。

我的问题是关于该变量的范围($ tmp)。在每一个电话功能中,它的价值都会被覆盖吗?换句话说,通过每个函数调用,我们都会失去先前的值?

有关详细信息,请查看以下代码:

/// --- { Declaration Block
$content = array(
    array(
        'level'=>'1',
        'order'=>'1',
        'text'=>'New Solution WorkFlow',
        'is_parent'=>'yes',
        'child'=> array(
            array(
                'level'=>'2',
                'order'=>'1',
                'text'=>'Define New Solution',
                'is_parent'=>'no',
                'url'=>'#'
            ),
            array(
                'level'=>'2',
                'order'=>'2',
                'text'=>'View Solutions',
                'is_parent'=>'no',
                'url'=>'#'
            ),
            array(
                'level'=>'2',
                'order'=>'3',
                'text'=>'View Solutions',
                'is_parent'=>'no',
                'url'=>'#'
            )
        )
    ),
    array(
        'level'=>'1',
        'order'=>'2',
        'text'=>'Solution Modify WorkFlow',
        'is_parent'=>'yes',
        'child'=> array(
            array(
                'level'=>'2',
                'order'=>'1',
                'text'=>'Request For Solution Modify',
                'is_parent'=>'no',
                'url'=>'#'
            )
        )
    ),
    array(
        'level'=>'1',
        'order'=>'3',
        'text'=>'Solution Close WorkFlow',
        'is_parent'=>'yes',
        'child'=> array(
            array(
                'level'=>'2',
                'order'=>'1',
                'text'=>'Declare For Solution Close',
                'is_parent'=>'no',
                'url'=>'#'
            )
        )
    )
);

function ExpMenu($item_array ) {

    $tmp='';
    foreach ($item_array as $item) {
        if ($item['is_parent']=='yes') {
            $tmp = '<li class="hasChild">' . $item["text"] . '<ul>';
            $tmp .= ExpMenu($item['child']);
            $tmp .= '</ul></li>';
        } else {
            $tmp = '<li>';
            $tmp .= '<a href="' . $item['url'] . '">'. $item['text'] . '</a>' ;
            $tmp .= '</li>';
        }
    }
    return $tmp;

}
/// --- }

$menu='<div><ul>';
$menu .= ExpMenu($content);
$menu.='</ul></div>';


echo $m . '<br />';

2 个答案:

答案 0 :(得分:2)

它通过每个呼叫功能接缝我们失去了过去的价值。

我感谢@l0rkaY他/她的解决方案,但我找到了另一个不需要在我的函数中添加新参数的解决方案。

因为$ tmp范围在&#39; ExpMenu&#39;函数和我们称之为递归函数,因此变量仍然存活并且没有被终止。

所以,我稍微修改了我的功能:

class func isConnectedToNetwork()->Bool{

    var Status:Bool = false
    let url = NSURL(string: "http://google.com/")
    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "HEAD"
    request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
    request.timeoutInterval = 10.0

    var response: NSURLResponse?

    var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?

    if let httpResponse = response as? NSHTTPURLResponse {
        if httpResponse.statusCode == 200 {
            Status = true
        }
    }

    return Status
}

答案 1 :(得分:0)

我假设您的实际问题是,您的函数仅生成具有单个子项的单个项目。

这是因为你在if / else块中覆盖了你以前的项目。这就是为什么你只得到最后一项。
您只需将它们连接到现有项目即可。

function ExpMenu($item_array ) {

    $tmp='';
    foreach ($item_array as $item) {
        if ($item['is_parent']=='yes') {
            $tmp .= '<li class="hasChild">' . $item["text"] . '<ul>';
            $tmp .= ExpMenu($item['child']);
            $tmp .= '</ul></li>';
        } else {
            $tmp .= '<li>';
            $tmp .= '<a href="' . $item['url'] . '">'. $item['text'] . '</a>' ;
            $tmp .= '</li>';
        }
    }
    return $tmp;

}