PHP Switch Statement with Example

PHP switch statement

The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression.

Syntax:

Working:

First you have a single expression “n” (most frequently a variable), that is evaluated once. The value of the expression is compared with the values for each case in the structure. If any match found, the block of code associated with will be executed. Use break to prevent the code from executing into the next case automatically. The default statements is used if no match is found.
PHP Switch Statement Flow Chart

Example:

Output:                

Example2:

Output:

Example3:

Output:

Operator Description Example
Less than ($age < 18)
Greater than ($age > 18)
<= Less than or equal to ($age <= 18)
>= Greater than or equal to ($age >= 18)
!= Not equal to ($age != 18)
== Equal to ($age == 18)

Example4:

Output:

Example5:

Output:
PHP Switch Statement example

Leave a Comment