switch statements php
The switch case statement in PHP is an alternative to multiple if/elseif condition statements when programmers need to compare objects to a value in a single program, using a condition, logic, or multiple expressions. The switch statement helps programmers develop clear code and easy-to-use, read logic when programmers need to test multiple conditions or logic expressions on a single variable.

Syntax of a switch statement in PHP
switch (logic expression) {
case result1:
/here it runs program code to be executed if logic expression == result1
break;
case result2:
/here it runs program code to be executed if logic expression == result2
break;
case result3:
/here it runs program code to be executed if logic expression == result3
break;
default:
// here it runs the program. Code to be executed if no one of the above cases matches the logic or expression.
}
Explanation of a switch statement in a PHP program.
- Logic expression – This switch case statement is the value you are testing in multiple switch statements.
- Case value – This is the probable value of the output of the expression in the switch statement. If the current switch case statement matches a given case expression, it executes the source code block in the corresponding switch case statement.
- break keyword – This switch case statement breaks the source code from executing other cases after the logic expression in the switch case statement matches. If the user does not use the break keyword, PHP will continue executing the next matched case statement, which is called a “fall-through” condition.
- default keyword – If none of the given case logic expressions in a switch case statement matches, the default statement is executed at the end. This is an optional option, but it is useful as a fallback in a switch case statement.
Example of a switch statement.
<?php
$weekday = 1;
switch ($weekday) {
case 1:
echo “Monday”; // Result – Monday
break;
case 2:
echo “Tuesday”;
break;
case 3:
echo “Wednesday”;
break;
case 4:
echo “Thursday”;
break;
case 5:
echo “Friday”;
break;
case 6:
echo “Saturday”;
break;
case 7:
echo “Sunday”;
break;
default:
echo “Invalid weekday”; // This default statement will only execute when all the above expressions are false
}
?>
Here’s an example of a switch case statement.
In the switch case statement, the logic expression $weekday = 1 is evaluated, and the switch case statement compares it to other switch case values in the program.
Because the switch case returns 1, the output is “Monday”.
After a match is made to a case statement, the break statement ensures that no further cases are evaluated.
Example of the use of the default statement in a switch.
The default case is an optional choice in a switch case statement, but it’s useful when you want to manage switch case statement values that don’t match any of the case values.
<?php
$laptop = “asus”;
switch ($laptop) {
case “hp”:
echo “This is an hp laptop”;
break;
case “lenovo”:
echo “This is a leovo laptop”;
break;
case “macbook”:
echo “This is a macbook laptop”;
break;
default:
echo “This is not a match for any laptop category”; // Result – This is not a match for any laptop category
}
?>
Here’s an example of a default statement.
The switch case statement contains $laptop “asus”, which doesn’t match any of the given case expressions, so the default statement block of code is executed.
Example of a switch with multiple cases.
In a PHP program, programmers can use multiple case values within a single case statement by separating them with the : (colon) operator. A multiple switch case is useful when programmers need multiple values to trigger the same program source code.
<?php
$programming = “java”;
switch ($programming) {
case “python”:
case “java”:
echo “this is a java program”; // Result – this is a java program
break;
case “matlab”:
echo “this is a matlab program.”;
break;
default:
echo “there is no programming category”;
}
?>
Here in this switch with multiple cases example,
In this multiple switch case, both the “python” and “java” switch case statements will execute the same output value.
The break statement here ensures that only the matching program block is executed.
Fall-through switch case Behaviors (without break) example.
If the programmer omits the break statement in a switch case statement, PHP will continue to execute the case blocks in subsequent switch case statements in the program, even if they no longer match the current condition. This is called fall-through behavior in a switch case statement.
<?php
$desktop = “apple imac”;
switch ($desktop) {
case “apple imac”:
echo “this is an Apple imac desktop”;
case “hp desktop”:
echo “\n”;
echo “this is an hp desktop”;
echo “\n”;
case “lenovo desktop”:
echo “this is a lenovo desktop”; // Result – this is an Apple imac desktop, this is an hp desktop, lenovo desktop
break;
default:
echo “Unknown desktop category.”;
}
?>
Here in this fall-through switch case example.
Since $desktop = “apple imac”; is used here, the first case block is executed.
However, there is no break keyword or statement after the first echo, so the program continues executing the second and third cases even if none of the case expressions match.
Example of a switch with data types other than strings or integers.
The switch case statement can work with multiple data types, such as strings, integers, and floats. The switch case statement also works with boolean and array data types, but remember that PHP switch case statements follow strict comparison (===) rules, which test both the value and the data type in the current program.
Example with a switch case string example.
<?php
$programming = “javascript”;
switch ($programming) {
case “javascript”:
echo “this is a javascript”;
break;
case “html”:
echo “this is a html”;
break;
case “css”:
echo “this is a css”;
break;
default:
echo “Unknown programming selection”;
}
?>
Example with a switch case boolean.
<?php
$is_status = true;
switch ($is_status) {
case true:
echo “You are log in system”;
break;
case false:
echo “You are log out system”;
break;
}
?>
Example with switch case floats.
<?php
$salary = 1099.80;
switch ($salary) {
case 1099.80:
echo “The salary is $1099.80”; // Result – The salary is $1099.80
break;
case 399.77:
echo “The salary is $399.77”;
break;
default:
echo “unknown salary selection”;
}
?>
Explanation of switch Statement in php
| Switch case statement | Switch case Description |
| switch(expression) | It used to Evaluates the switch case expression for comparison until match |
| case value: | It used to Compares the switch case expression to value. Once they match, that match case is execute |
| break; keyword | It used to Exits the switch statement and prevents further case checks or run |
| default: keyword | It used to Executes when no case matches the expression (optional) then default keyword used. |
Summary of the switch case statement in PHP.
- Strict comparison (===) – The PHP switch case statement follows strict comparison, meaning it tests both the value and the data tie in the program. This is important if programmers are comparing multiple data types, such as strings versus integers.
- Multiple case values - PHP programmers can combine multiple case values together for switch case statement general logic.
- No break – Omitting the break keyword in a PHP switch case statement produces fall-through Behavior, where all subsequent case blocks are automatically executed one by one, even if the logic value doesn’t match the switch case statement.

