PHP switch statement is used when one of the several options or a groups of options are to be executed based on some conditions. We can use several PHP if conditions with php if else but this is a better way of coding when we know one of the several options is correct. (Repeated checking by if condition can be avoided)
If you want to select one of many blocks of code to be executed, use the Switch statement.
The switch statement is used to avoid long blocks of if..elseif..else code.
Syntax
PHP Code:
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
Bookmarks