0

好的,这可能是一个基本问题,但我想要一个明智的答案,而且我似乎无法通过谷歌获得正确的关键字来获得答案。如果这是重复的,请关闭它并指出正确的方向。

所以这就是问题所在。在下面的代码中,当所指向的 break 被执行时,它也在哪里发生了?

case 'changeEmployeeInfo':
    $con = db_connect(DBNAME, DBUSERNAME, DBPASSWORD , DBHOST);
    $query = 'UPDATE USERS SET firstname = ?, lastname = ?, email = ? where idusers = ?';
    $updateValues = array($firstName,$lastName,$email,$employeeID);
    $newID = db_change($query,$updateValues,$con);
    if($_SESSION['role']==1||$_SESSION['role']==3||$_SESSION['role']==4){
        if($_POST['status']== true){
            $status = 1;
        }elseif($_POST['status']==false){
            $status = 0;
        }
        else{
            break;//<--THIS IS THE BREAK I'M TALKING ABOUT
        }
        $query = 'UPDATE USERS SET status = ? where idusers = ?';
        $updateValues = array($status, $employeeID);
        $newID = db_change($query,$updateValues,$con);
    }
    db_disconnect($con);
    break;

我的直觉告诉我该db_disconnect函数仍将被执行,但UPDATE USERS查询及其相关行将不会被执行。我这样想对吗?谢谢!

4

3 回答 3

5

break在 PHP 中恰好打破了一个级别,在您的情况下是case. 要打破更多关卡,请使用break n

文档中:

$i = 0;
while (++$i) {
    switch ($i) {
    case 5:
        echo "At 5<br />\n";
        break 1;  /* Exit only the switch. */
    case 10:
        echo "At 10; quitting<br />\n";
        break 2;  /* Exit the switch and the while. */
    default:
        break;
    }
}
于 2013-01-09T21:06:54.347 回答
2

当 PHP 中达到一个突破时,它突破了多少个级别?

1

于 2013-01-09T21:05:59.187 回答
1

In your example, break will move out to the switch level, leaving your case statement. db_disconnect will not be run.

于 2013-01-09T21:08:01.853 回答