switch case Statement in PHP
Notes:
PHP switch case statement:
When there are more than one alternative choices present, we use switch case statement.
PHP engine executes the case whose label matches the expression given in a switch parenthesis.
syntax:
switch(expression)
{
case label-1:
statements;
break;
case label-2:
statements;
break;
…
default:
statements;
break;
}
OR
switch(expression):
case label-1:
statements;
break;
case label-2:
statements;
break;
…
default:
statements;
break;
endswitch;
Example Code:
$day = -10;
switch($day)
{
case 1: echo "Sunday","<br/>";
break;
case 2: echo "Monday","<br/>";
break;
case 3: echo "Tuesday","<br/>";
break;
case 4: echo "Wednesday","<br/>";
break;
case 5: echo "Thursday","<br/>";
break;
case 6: echo "Friday","<br/>";
break;
case 7: echo "Saturday","<br/>";
break;
default: echo "Invalid day","<br/>";
break;
}
echo "Outside switch";
OR
$day = 4;
switch($day):
case 1: echo "Sunday","<br/>";
break;
case 2: echo "Monday","<br/>";
break;
case 3: echo "Tuesday","<br/>";
break;
case 4: echo "Wednesday","<br/>";
break;
case 5: echo "Thursday","<br/>";
break;
case 6: echo "Friday","<br/>";
break;
case 7: echo "Saturday","<br/>";
break;
default: echo "Invalid day","<br/>";
break;
endswitch;
echo "Outside switch";
Interview Questions:
1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a