This statement is used if one needs to select one from multiple options.
PHP Switch Case Syntax:
switch (x) {
case label1:
code to be executed if x=label1;
break;
case label2:
code to be executed if x=label2;
break;
case label3:
code to be executed if x=label3;
break;
...
default:
code to be executed if x is different from all labels;
}
- PHP switch case is used to select one among multiple options.
- inside switch we can use expression or value
- inside switch there can be multiple case statement with different case value.
- case value must be constant.
- break is optional in php switch case
- Default is optional in php switch
- If any case does not match then default is executed.
Example: PHP Switch case statement
Result:
I booked Swift