PHP switch Statement
The command switch
is used for performing various actions on a range of conditions.
You can choose one of the multiple blocks of code to execute by using the switch
statement.
Syntax :-
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
Related Links
This is how it works :
- Initially, we have the single term n, which is evaluated once, most often a variable.
- The expression's value is then compared to the values in the structure for each circumstance.
- If a match is found, the case-specific associated with that case is run.
- Use
break
to avoid that code is automatically launched into the following instance. - If no match is discovered, the
default
statement is used.
Example :-
Output :-
Your favorite color is red!
Related Links
You can also search for these topics, php switch statement, php switch case, php switch multiple case, php switch case return, php switch case conditions, php switch case syntax, php switch case example, php switch case working method, php switch case default.