
The switch case statement in PHP
As we know, the if-else statement is used to check and branch the logic of a problem. However, it is not the only option. In PHP, there is another statement called switch case that can be used for branching.
In this article, we will learn about three main topics:
- The switch statement
- Switch vs if
- Nested switch statements
1. The switch statement in PHP
The switch statement in PHP allows us to make decisions with multiple choices based on the value of an expression. If the value of the expression matches any of the conditional expression values, the statements inside the conditional expression will be executed.
Syntax:
<?php switch ($variable) { case $value_1: // statement block break; case $value_2: // statement block break; default: // statement block break; } ?>
In which the switch, case, and default statements are keywords in PHP. The statement blocks can be a single statement or a compound statement (a group of statements) and do not need to be enclosed in curly braces {}. In each choice (case) statement, it compares whether the variable passed $variable
is equal to the conditional variable $value_1
, $value_2
, etc. If it matches any of the cases, the statements inside that case will be executed, and the break statement will end the switch statement. If there is no match, it will run the statement block inside the default statement in the switch statement, which may or may not be present.
The value in the case only accepts string, INT, boolean, null, float data types or an expression that returns one of these data types. The comparison operator used in the switch statement is always ==.
Example: Write a program that takes an input number and uses the switch statement to check if the number is:
- Equal to 0, then print “Number zero”
- Equal to 1, then print “Number one”
- Equal to 2, then print “Number two”
- Equal to 3, then print “Number three”
- Equal to 4, then print “Number four”
- For any other number, print “Not found”
<?php $number = 2; switch ($number) { case 0: echo "Number zero"; break; case 1: echo "Number one"; break; case 2: echo "Number two"; break; case 3: echo "Number three"; break; case 4: echo "Number four"; break; default: echo "Not found"; break; } ?>
Explanation:
- Step 1: The expression passed in is the variable
$number
with a value of 10. - Step 2: In the first case, the condition is that
$number
equals 0 for it to be executed, but$number
equals 10, so it does not meet the condition. - Step 3: Similarly, none of the remaining case statements meet the condition.
- Step 4: It reaches the default statement, which is executed when none of the above case statements meet the condition, so “Not found” will be displayed on the screen.
Assuming we input $number = 2
, the problem above can be explained as follows:
- Step 1: Enter
$number = 2
- Step 2: In the first case, it does not meet the condition because 2 is not equal to 0, so the program will move to the next case.
- Step 3: In the second case, it still does not meet the condition because 2 is not equal to 1, so the program will move to the next case.
- Step 4: In this third case, the condition is met because 2 equals 2, and now the program will execute the commands inside the case, so “Number two” will be displayed on the screen. Additionally, the Break statement will stop the entire switch statement since the condition has been met.
- The program ends.
2. Switch and If
If statement and Switch statement are two types of control flow statements in PHP, however, If statement is still more flexible than Switch and faster as well. For problems that can be expressed using Switch, it is entirely possible to convert them to If statements, but for problems that are expressed using If statements, it may not be possible to convert them to Switch statements.
For the problem above, we can express it using If statement as follows:
<?php $number = 10; if ($number == 0){ echo 'Zero'; } else if ($number == 1){ echo 'One'; } else if ($number == 2){ echo 'Two'; } else if ($number == 3){ echo 'Three'; } else if ($number == 4){ echo 'Four'; } else { echo 'Not found'; } ?>
3. Nested Switch Statement
Similar to the if statement, the switch statement can also be nested.
Example:
<?php $number = 12; $midle = null; switch ($number){ case 12 : // if $number = 12 $midle = $number % 2; // get the remainder switch ($midle) { case 0 : // if remainder = 0 echo 'Even number'; break; default : echo 'Odd number'; break; } break; default: // if not 12 then do nothing break; } ?>
3. Conclusion
In this lesson, I hope you have understood how to use the switch case statement in PHP and have some clear decisions in choosing between the switch and if statements. In the next lesson, we will study loops. There are 4 types of loops in PHP, but we will study the for loop first.
Related Articales
Comments
-
Download and Install Vertrigo Server
May 25, 2023.84 views -
Declaring variables in PHP, common types of variables encountered
May 25, 2023.84 views -
Data types in PHP and their corresponding variable types
May 25, 2023.84 views -
Operators and expressions in PHP
May 25, 2023.84 views -
The if else statement in PHP
May 25, 2023.84 views -
The switch case statement in PHP
May 25, 2023.84 views -
The for loop in PHP
May 25, 2023.84 views -
While and do-while loops in PHP
May 25, 2023.84 views -
The foreach Loop in PHP
May 25, 2023.84 views -
The break, continue, goto, die, exit statements in PHP
May 25, 2023.84 views -
Building Functions in PHP
May 25, 2023.84 views -
Recursive Algorithm in PHP
May 25, 2023.84 views -
Bubble Sort Algorithm in PHP
May 25, 2023.84 views -
Linear Search Algorithm in PHP
May 25, 2023.84 views -
The technique of sentry placement in PHP
May 25, 2023.84 views -
Flagging technique in PHP
May 25, 2023.84 views -
Selection Sort Algorithm in PHP
May 25, 2023.84 views
Tags
© copyright 2021 Courseplus