对于循环在第一次迭代后不继续

时间:2017-10-13 02:42:40

标签: php

所以我编写了如下所示的for循环,由于某种原因,在第一次循环之后,循环停止。在这种情况下,我试图发送值4,并且它失败了从checkRes函数检查字符串“true”的if语句。这就是函数所做的就是返回字符串。我对循环的理解是它将继续循环,直到满足顶部的语句,或者它被退出。  我在下面做错了什么?

 // assume $avaliable = 4;
 for ($i=$avaliable;10>$i;$i++) {
    $check = checkRes($i, $people_no, $booking_date);
        if ($check === "true") {
            switch($i) {
                case 0: $newResTime = "6 PM"; break;
                case 1: $newResTime = "6:30 PM"; break;
                case 2: $newResTime = "7 PM"; break;
                case 3: $newResTime = "7:30 PM"; break;
                case 4: $newResTime = "8 PM"; break;
                case 5: $newResTime = "8:30 PM"; break;
                case 6: $newResTime = "9 PM"; break;
                case 7: $newResTime = "9:30 PM"; break;
                case 8: $newResTime = "10 PM"; break;
                case 9: $newResTime = "10:30 PM"; break;
                case 10: $newResTime = "11 PM"; break;
            }
            // Replace next line with your return from the chatbot
            echo "We're sorry, that time isn't avaliable, but a reservation at $newResTime has been made!";
            exit;
        }
    }

1 个答案:

答案 0 :(得分:2)

您目前正在使用exit作为"默认"解。但它总是终止程序的执行(因为那是exit所做的)。

而是这样做

switch($i) {
    case 0: $newResTime = "6 PM"; break;
    case 1: $newResTime = "6:30 PM"; break;
    case 2: $newResTime = "7 PM"; break;
    case 3: $newResTime = "7:30 PM"; break;
    case 4: $newResTime = "8 PM"; break;
    case 5: $newResTime = "8:30 PM"; break;
    case 6: $newResTime = "9 PM"; break;
    case 7: $newResTime = "9:30 PM"; break;
    case 8: $newResTime = "10 PM"; break;
    case 9: $newResTime = "10:30 PM"; break;
    case 10: $newResTime = "11 PM"; break;
    default: echo "..."; break;