In PHP, we use a switch statement for the execution of one statement by specifying multiple conditions. It is an alternative to the if-else if- else statement. Both statements are serving the same purpose as conditional statements.
The switch statement in PHP is also known as a switch-case. Different blocks of code are executed for a different number of cases along with the default code block with the switch-case statement.
This article will explain different examples for PHP switch-case statements. There are example code snippets along with outputs.
Table of Contents
PHP Switch Syntax:
The syntax rule has to be followed while using switch-case statement.
switch(expression){
case name1:
// Execute this code when expression=name1
break;
case name2:
// Execute this code when expression=name2
break;
...
default:
// Default statement will be executed if none of the cases are matched with the expression
}
In the Switch block, we compare the expression with each of the case’s values. We can use a break statement after each case for not letting the whole code be executed. The code will run until it finds the true case to be executed. The break statement is optional.
If there is no true match for switch expression and any of the cases, PHP executes a default code block.
We evaluate the switch conditional statement for once only. Afterward, each case’s value is matched with the value of the expression in the Switch block. In the output, it will either execute the associated code or the default block of code.
We can nest switch statements, but it will make our code complex.
Example Code: 01:
The following example demonstrate how to use PHP Switch with String.
<?php
$name= 'Code Leaks';
switch($name){
case 'site':
echo("You have chosen wrong word here.");
break;
case 'Code Leaks':
echo("Welcome to Code Leaks. Learn with us.");
break;
case 'codeleaks':
echo(" Code is case sensitive.");
break;
default:
echo("None of the above returns Code Leaks.");
}
?>
Output:

Example Code: 02:
The PHP Switch with numbers is executed below.
<?php
$number=10;
switch($number){
case 0:
echo("Number is equals to 0");
break;
case 05:
echo("Number is equal to 05");
break;
case 10:
echo("Number is equal to 10");
break;
default:
echo("Number is not equal to 0, 05 or 10");
}
?>
Output:

Example Code: 03:
This example explains how to nest PHP Switch statement.
<?php
$country = "US";
$city = "Chicago";
switch( $country )
{
case "Turkey":
switch( $city )
{
case "Istanbul":
echo "Istanbul is the capital of Turkey.";
break;
case "Konya":
echo "Konya has the largest fresh water lake.";
break;
}
break;
case "US":
switch( $city )
{
case "New York":
echo "More than 800 languages are spoken in New York.";
break;
case "Chicago":
echo "Chicago has got 37 movable bridges.";
break;
case "Denver":
echo "Denver has a unique city park system";
break;
}
break;
}
?>
Output:

Example Code: 04:
The below example shows the output of PHP Switch with no break statement.
<?php
$character = 'A';
switch ($character)
{
case 'E':
echo "Choice E";
break;
case 'I':
echo "Choice I";
break;
case 'A':
echo "Choice A";
echo "</br>";
case 'O':
echo "Choice O";
echo "</br>";
default:
echo "A, E, I, and O are not present.";
}
?>
Output:

How is Switch different from if-else if- else?
The thing that makes switch-case different from if-else if- else is the execution sequence. In the switch statement, code is executed line by line, and after finding the true case, it does not stop until all the subsequent case statements are not executed. In comparison, the execution is different in the if-elseif-else statement.
You can use a break statement at the end of every case to avoid the code execution till the end. It will just execute the true case in the switch block.
Conclusion:
The article has discussed the PHP switch-case statement in detail. I hope the examples were quite helpful in understanding how to use the PHP switch statement.