22
switch ($var) {
    case 0:
        // Do something...
        break;
    case 1:
        // Do something...
        break;
    default:
        // Do something...
        break;
}

I've seen some people use break at the end of the default case. Since the default case is the last case that's executed when triggered, is there any need to have a break there? I'm guessing it's just done out of common practice or is there another reason?

4

1 に答える 1

34

デフォルトが switch ステートメントの最後にある限り、必要な理由はありません。デフォルトが最後のケースである必要はないことに注意してください: http://codepad.viper-7.com/BISiiD

<?php
$var = 4;
switch($var)
{
    default:
        echo "default";
        break;
    case 4:
        echo "this will be executed";
        break;
}
于 2012-08-21T03:18:19.860 に答える