If Else and Switch Case
If Else and Switch Case are control structures that let you execute code segments based on given conditions. These build up the dynamic behavior of PHP.
If
Think that you are writing a PHP program for a store and you have some offers based on the age. Following is a single if condition that would show some special offers if the user is a teenage. It won’t show any offer if the user belongs to any other age group.
if (($age > 12) && ($age < 20)) {
// Show teenage offers
}
If Else
Think that you have got some general offers to other age groups. Then you can attach an else statement to show those offers if the user is not a teenage.
if (($age > 12) && ($age < 20)) {
// Show teenage offers
} else {
// Show general offers
}
If Elseif
If you are going to introduce some adult offers (say for age over 40), you can use an elseif and have another condition to check whether the age is above 40.
if (($age > 12) && ($age < 20)) {
// Show teenage offers
} elseif ($age > 40) {
// Show adult offers
} else {
// Show general offers
}
Above code would first check whether the user is a teenage, if not, it would check whether if he is an adult. If that is also false, it would go to the else block. Like this, you can have more elseif blocks if you have offers for more age groups. Having else block at the end is optional. You can decide not to show any offer if the user doesn’t belong to any of your defined age groups.
Condition of If Statements
Note that condition of if statements should always result in either true or false. If the result is true, immediate code block would be executed. If it is false, control will go to next elseif condition or else block depending on their availability.
Switch Case
Switch Case acts similar to Elseif control structure. Main difference is that Switch Case can be written in a way that it compares a given value with a set of predefined values without evaluating set of conditions.
$day = 'Wednesday';
switch ($day) {
case 'Monday':
echo 'Wish you a fresh start!';
break;
case 'Wednesday':
echo 'Keep going. Good luck!';
break;
case 'Friday':
echo 'Happy weekend!';
break;
default:
echo 'Good day!';
break;
}
You can see that there is no condition to evaluate. Same result could have been achieved using an Elseif but with three condition evaluations.
$day = 'Wednesday';
if ($day == 'Monday') {
echo 'Wish you a fresh start!';
} elseif ($day == 'Wednesday') {
echo 'Keep going. Good luck!';
} elseif ($day == 'Friday') {
echo 'Happy weekend!';
} else {
echo 'Good day!';
}
When using Switch Case, remember to use break statement consistently. Not having it can lead to unexpected results. default is the code block that would be executed if the given value is not matched with any of the pre-defined values. It acts similar to Else in If control structures and is optional.
If Else vs Switch Case
If Else can be used for any occasion where comparisons need to be done. Each If statement checks a condition and operands associated with the condition can be different from one If to another attached Elseif.
Switch Case is good to use when the same value is compared against a set of values. In Switch Case, there are no conditions to evaluate so it can be bit efficient over an Elseif counterpart.
$day = 'Friday';
$longShift = true;
if ($day == 'Monday') {
echo 'Wish you a fresh start!';
} elseif ($day == 'Wednesday') {
echo 'Keep going. Good luck!';
} elseif ($day == 'Friday' && !$longShift) {
echo 'Happy weekend!';
} else {
echo 'Good day!';
}
In above case, an additional variable is involved. So, we can’t use usual Switch Case for this. But we can write it using Switch Case as below which is not common since Switch Case is often used to compare a single value and Elseif is used if different conditions are involved. We pass Boolean true to the Switch so it would always run inside code block.
$day = 'Friday';
$longShift = true;
switch (true) {
case ($day == 'Monday'):
echo 'Wish you a fresh start!';
break;
case ($day == 'Wednesday'):
echo 'Keep going. Good luck!';
break;
case ($day == 'Friday' && !$longShift):
echo 'Happy weekend!';
break;
default:
echo 'Good day!';
break;
}
