The SWITCH - CASE - DEFAULT statement is very helpful in case when a script contains multiple conditions to be observed and resolved. This statement starts with a single statement, compare the given condition with one of the label inside the SWITCH construct and via a CASE keyword. To prevent the parser from exiting the statment tree without a proper execution, a keyword DEFAULT also may be used. This will ensure that someting gets done even if no conditions have been met.

The break statament

The execution of SWITCH - CASE statement has to be stopped by using a BREAK statement. If the BREAK statement is omitted, the parser will execute the correct CASE, depending on the condition, and move onto the next one, and so on till it reaches the first BREAK, DEFAULT or the SWITCH statement exit bracket (}).

Syntax for SWITCH - CASE - DEFAULT statement with BREAK

<?php
switch (condition) {
    case 1:
        // first statement execution
        break;
    case 2:
        // second statement execution
        break;
    default:
        // default statement execution
}
?>

Example with a SWITCH - CASE - DEFAULT statement with BREAK

 

›› go to examples ››