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条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?