PHP函数 - 注意:未定义的变量问题[关闭]

I created two functions both functions used to work before I turned them into functions and now I get the following errors Notice: Undefined variable: link, url and depth and I was wondering how can I fix this problem so they can work. The problem comes from the last piece of code allCategories($link[0], $url, 0, $depth+1); which I believe stems from the second function.

Function 1

function allCategories($parent = 0, $parent_url = '', $count = 0, $depth = 0){
    catQuery();
    global $link;           
    if($count == 0){
        echo '<ol>';
    } else {
    echo '<ol>';
    }

    foreach ($parent as $id => $cat) {
    if($cat['parent_id'] == '0'){
            $url = $parent_url . $cat['url'];
            $url = str_replace('&#063;cat=', '', $url);
            echo '<li><a href="http://www.example.com/cat/' . strip_tags($url) . '" title="' . strip_tags($cat['category']) . ' Category Link" class="category-headers">' . strip_tags($cat['category']) . '</a>';          
    } else {
            $indent = str_repeat('&nbsp; ', $depth * 1);
            $url = $parent_url . $cat['url'];
            $cat_num = array('&#063;cat=','&#038;sub1=','&#038;sub2='');
            $url = str_replace($cat_num, '/', $url);
            echo '<li>' . $indent . '<a href="http://www.example.com/cat/' . strip_tags($url) . '" title="' . strip_tags($cat['category']) . ' Category Link">' . strip_tags($cat['category']) . '</a>';
    }

        if(isset($link[$id])) {
            allCategories($link[$id], $url, $count+1, $depth+1);
    }               
            echo '</li>';
    }       
    echo '</ol>';
}

Function 2

function catQuery(){
    $cat_dbc = mysqli_query(database(),"SELECT * FROM categories ORDER BY parent_id, category LIKE '%more%', category ASC");
    if (!$cat_dbc) {
            trigger_error(mysqli_error(database()));
    }

    $link = array();
    while(list($id, $parent_id, $category, $url, $depth) = mysqli_fetch_array($cat_dbc)){
            $link[$parent_id][$id] = array('parent_id' => $parent_id, 'category' => $category, 'url' => $url, 'depth' => $depth);
    }
}

Display the desired output.

allCategories($link[0], $url, 0, $depth+1);

The answer Write code that does not try to access undefined variables. If you are a beginner, you might want to read a recent book about PHP5.

In your second function, you forgot global $link;. However, better return $link; and use $link = catQuery(); in your first function instead of this horrible abuse of global variables

For the other undefined vars.. did you define $link, $url and $depth before calling allCategories($link[0], $url, 0, $depth+1);?

change the end of function catQuery like this

    while(list($id, $parent_id, $category, $url, $depth) = mysqli_fetch_array($cat_dbc)){
            $link[$parent_id][$id] = array('parent_id' => $parent_id, 'category' => $category, 'url' => $url, 'depth' => $depth);
    }
   return $link;
}

then change the start of the allCategories function like this

function allCategories($parent = 0, $parent_url = '', $count = 0, $depth = 0){
    $link = catQuery();
    // global $link;           
    if($count == 0){

the reason why it didn't work is that you would need to have "global $link;" defined in each function. but global variables are not good practice, so best to return $link from catQuery function and then use it locally inside the allCategories function