我有一个递归打开 HTML 页面并提取文章的函数。执行该函数后,返回的数组为 NULL,但我的跟踪步骤表明该数组实际上包含元素。我相信在返回数组时它会被重置。
为什么数组包含函数中的元素,但返回后为NULL?
这是函数(简化):
function get_content($id,$page=1){
global $content; // store content in a global variable so we can use this function recursively
// If $page > 1 : we are in recursion
// If $page = 1 : we are just starting
if ($page==1) {
$content = array();
}
$html = $this->open($id,$page)) {
$content = array_merge($content, $this->extract_content($html));
$count = count($content);
echo("We now have {$count} articles total.");
if($this->has_more($html)) {
$this->get_content($id,$page+1);
} else {
$count = count($content);
echo("Finished. Found {$count} articles total. Returning results.");
return $content;
}
}
这就是我调用函数的方式:
$x = new Extractor();
$articles = $x->get_content(1991);
var_export($articles);
此函数调用将输出如下内容:
We now have 15 articles total.
We now have 30 articles total.
We now have 41 articles total.
Finished. Found 41 articles total. Returning results.
NULL
为什么数组包含函数中的元素,但返回后为NULL?