Decision Making Statement in PHP Using Switch

Decision Making Statement in PHP Using Switch

  • PHP
  • 3 mins read

Switch used to select one of many blocks of code to be executed. It is an alternative way to the if-elsif-else statement. Here I am giving some examples of decision-making statement in PHP using Switch command.

The general syntax of the switch is given below:

switch(expression)
{
case expression1:
block of code to be executed;
break;

case expression2:
block of code to be executed;
break;

case expression3:
block of code to be executed;
break;
...
default:
block of code to be executed;
break;
}

Here expression will be evaluated once. If the value of the expression is matched with any case expression, then the block of code associated with that case is executed.

If the value of the expression is not matched with any case expression, then the block of code associated with the default case will be executed.

Here break is used to prevent the code from running into the next case automatically.

1. Program on switch case in PHP

<html>
<head>
<?php
$a=1;
switch($a)
{
case 1:
echo "January";
break;
case 2:
echo "February";
break;
case 3:
echo "March";
break;
case 4:
echo "April";
break;
case 5:
echo "May";
break;
case 6:
echo "June";
break;
case 7:
echo "July";
break;
case 8:
echo "August";
break;
case 9:
echo "September";
break;
case 10:
echo "October";
break;
case 11:
echo "November";
break;
case 12:
echo "December";
break;
}
?>
</body>
</html>

When switch expression is evaluated, then it will return a value(1). That value will match with the first case expression. So "January" will be printed.

2. Program on switch case

<html>
<head>
<?php
$a="john";
switch($a)
{
case "john":
echo "khardah";
break;
case 2:
echo "Kolkata";
break;
case 3:
echo "Delhi";
break;
case 4:
echo "Italy";
break;
case 5:
echo "Barrackpore";
break;
default:
echo "well come";
}
?>
</body>
</html>

When switch expression is evaluated, then it will return a value("john"). That value will match with the first case expression. So "khardah" will be printed.

3. Program on switch case

<html>
<head>
<?php
$a="john";
switch($a)
{
case "shilu":
echo "khardah";
break;
case 2:
echo "Kolkata";
break;
case 3:
echo "Delhi";
break;
case 4:
echo "Italy";
break;
case 5:
echo "Barrackpore";
break;
default:
echo "well come";
}
?>
</body>
</html>

When switch expression is evaluated, then it will return a value("john"). That value will not match with any case expression.

So default statement will be executed and "well come" will be printed.

4. Program on switch case

<head>
<?php
$a=1;
switch($a)
{
case 1:
echo "khardah";
case 2:
echo "Kolkata";
case 3:
echo "Delhi";
case 4:
echo "Italy";
case 5:
echo "Barrackpore";
default:
echo "well come";
}
?>
</body>
</html>

When switch expression is evaluated, then it will return a value(1). That value will match with the case expression 1. But there are no break statements for any case expression. So all block of code of each case expression will be executed. So the output will be "khardahkolkataDelhiItalyBarrackporewell come."