PHP Switch Case Statement Syntax and Example

This statement is used if one needs to select one from multiple options.

PHP Switch Case Syntax:

  1. PHP switch case is used to select one among multiple options.
  2. inside switch we can use expression or  value
  3. inside switch there can be multiple case statement with different case value.
  4. case value must be constant.
  5. break is optional in php switch case
  6. Default is optional in php switch
  7. If any case does not match then default is executed.

Example: PHP Switch case statement

Result:
I booked Swift

Switch Statement in JavaScript

The switch statement performs different actions based on different condition.

It can be called as the replacement of multiple nested if-else statements.

The JavaScript Switch Statement

  • The switch statement is used to select one of many code blocks to be executed.
  • Switch case is useful for menu based application.

Syntax

How Switch Statement works?

  • At first, the switch expression is evaluated once.
  • The value of the expression is compared with each of the case.
  • If there is a match, the associated block of code is executed i.e., only one case will be executed.
  • If break is not written after each of the code blocks, all the cases are executed after execution of the matched case.
  • The default portion is executed only when the user inputs a wrong value(other than the case values).

The getDay() method returns the weekday as a number within the range 0-6.
(Sunday=0, Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6)

Example