为什么我的递归函数不起作用?

时间:2010-02-18 11:13:45

标签: php recursion

$startDate = 20130201;
$date      = 20130505;
$aDates    = $this->getDates($startDate, $date);

public function getDates($startDate, $date) {
    $tmpStartDate = date("Ymd", strtotime($startDate.'+1 Day'));
    $tmpEndDate   = date("Ymd", strtotime($tmpStartDate.'+1 Month'));

    if($date >= $tmpStartDate && $date <= $tmpEndDate) {
        //return array('startDate' => $tmpStartDate, 'endDate' => $tmpEndDate);
    } else {
        $this->getDates($tmpEndDate, $date);
    }
}

1 个答案:

答案 0 :(得分:8)

如果调用了} else {,则递归调用不会返回任何内容。

尝试取消注释您的注释行并将“return”添加到该else子句的开头:

return $this->getDates($tmpEndDate, $date);