doushi8186 2016-08-09 19:43
浏览 156
已采纳

PHP函数显示结果但不返回它

I'm writing a code in PHP to find the next business day withing off days. This is my code:

function find_next_day($today,$days_array) {
    $day = new DateTime($today);
    $tomorrow = $day->modify('+1 day');
    $result = $tomorrow->format('Y-m-d');
    if (!in_array($result,$days_array)) {
        return $result;
    } else {
        find_next_day($result,$days_array);
    }
}

$off_days = array('2016-08-10','2016-08-25','2016-08-09','2016-08-11');
echo find_next_day('2016-08-09',$off_days); // I must get returned value as 2016-08-12

It's really strange that function does not return anything. But if I put echo $result before returning value in function, I can see the final result correctly!
What is my mistake here ?

  • 写回答

3条回答 默认 最新

  • duanbu4962 2016-08-09 19:47
    关注

    The problem is that you are not returning the result in the function in the else statement.

    function find_next_day($today,$days_array) {
        $day = new DateTime($today);
        $tomorrow = $day->modify('+1 day');
        $result = $tomorrow->format('Y-m-d');
        if (!in_array($result,$days_array)) {
            return $result;
        } else {
            return find_next_day($result,$days_array);
        }
    }
    

    Try this one out. Should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效