PHP program of decision making by using the switch case statement
ADVERTISEMENTS
PHP program of decision making by using the php switch case statement, In this program you will learn how to make decisions by using a php switch case statement.
- The php switch case statement is the same as the if..else if...else ladder in which the if..else if...else ladder making a decision among several conditions.
- The php switch case statement gives easy-to-match conditions & easy readability.
- The php switch case statement checks faster rather than the if..else if...else ladder.
Syntax of the php switch case statement
switch (e) {
case constant1:
// statements 1
break;
case constant2:
// statements 2
break;
.
.
.
default:
// default statements
}
The php switch case statement execution flow
- The php switch parameter matches conditions in every case to make a decision.
- There is if any case will be matched with the switch parameter then switch condition checking will be automatically stopped.
- There is if any cases don't match the condition then It navigates to the default case and the switch will be stopped.
Let us understand this statement through a php example:
<?php
// PHP program of decision making by using the php switch case statement
$w = 4;
if ($w > 0) {
switch ($w) {
case 1:
echo "Hi\nIt's sunday on today, It's the week-off day.\n";
break;
case 2:
echo "Hi\nIt's monday on today, It's the working day.\n";
break;
case 3:
echo "Hi\nIt's tuesday on today, It's the working day.\n";
break;
case 4:
echo "Hi\nIt's wednesday on today, It's the working day.\n";
break;
case 5:
echo "Hi\nIt's thursday on today, It's the working day.\n";
break;
case 6:
echo "Hi\nIt's friday on today, It's the working day.\n";
break;
case 7:
echo "Hi\nIt's saturday on today, It's the week-off day.\n";
break;
// w parameter doesn't match any case constant then It navigates to default:
default:
echo "Sorry, You entered the wrong week number, Kindly try again\n";
}
} else {
echo "Sorry didn't entered any week number!\n";
}
?>
Run Program
Output
Hi
It's wednesday on today, It's the working day.
It's wednesday on today, It's the working day.