This statement is used if one needs to select one from multiple options.
PHP Switch Case Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $Taxi = "swift"; switch ($Taxi) { case "red": echo "I booked Toyoto!"; break; case "blue": echo " I booked Swift!"; break; case "green": echo " I booked I10!"; break; default: echo "I booked either toyoto,swift or I10!"; } ?> |
Result:
I booked Swift