PHP 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.
1.if (($age > 12) && ($age < 20)) {
2.// Show teenage offers
3.}

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.
1.if (($age > 12) && ($age < 20)) {
2.// Show teenage offers
3.else {
4.// Show general offers
5.}

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.
1.if (($age > 12) && ($age < 20)) {
2.// Show teenage offers
3.elseif ($age > 40) {
4.// Show adult offers
5.else {
6.// Show general offers
7.}
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.
01.$day 'Wednesday';
02. 
03.switch ($day) {
04. 
05.case 'Monday':
06.echo 'Wish you a fresh start!';
07.break;
08. 
09.case 'Wednesday':
10.echo 'Keep going. Good luck!';
11.break;
12. 
13.case 'Friday':
14.echo 'Happy weekend!';
15.break;
16. 
17.default:
18.echo 'Good day!';
19.break;
20. 
21.}
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 you don’t specify a break statement in a case block then PHP will execute next blocks till it finds a break statement. You can use this behavior if you need to have same output for more than one case.
01.switch ($day) {
02. 
03.case 'Monday':
04.echo 'Wish you a fresh start!';
05.break;
06. 
07.case 'Tuesday':
08. 
09.case 'Wednesday':
10. 
11.case 'Thursday':
12.echo 'Keep going. Good luck!';
13.break;
14. 
15.case 'Friday':
16.echo 'Happy weekend!';
17.break;
18. 
19.default:
20.echo 'Good day!';
21.break;
22. 
23.}
In above code, if $day matches to Tuesday, Wednesday or Thursday, it will print ‘Keep going. Good luck!’.

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, since usually there are no conditions to evaluate, it can be bit efficient over an Elseif counterpart.
Though it’s not common, conditions can be used in Switch Case. Both Elseif and Switch Case below work the same way. Boolean true is passed to the Switch so that execution will always be passed to the inside code blocks.
01.$day 'Friday';
02.$longShift = true;
03. 
04.if ($day == 'Monday') {
05.echo 'Wish you a fresh start!';
06.elseif ($day == 'Wednesday') {
07.echo 'Keep going. Good luck!';
08.elseif ($day == 'Friday' && !$longShift) {
09.echo 'Happy weekend!';
10.else {
11.echo 'Good day!';
12.}
01.$day 'Friday';
02.$longShift = true;
03. 
04.switch (true) {
05. 
06.case ($day == 'Monday'):
07.echo 'Wish you a fresh start!';
08.break;
09. 
10.case ($day == 'Wednesday'):
11.echo 'Keep going. Good luck!';
12.break;
13. 
14.case ($day == 'Friday' && !$longShift):
15.echo 'Happy weekend!';
16.break;
17. 
18.default:
19.echo 'Good day!';
20.break;
21. 
22.}

Ternary Operator (Shorthand If/Else)

Ternary operator let us return one of two values based on a given condition. It’s syntax is like below.

(expression)?(if expression is true):(if expression is false)

In following example, if current time is less than 12 noon, it will print Good Morning! else it will print Welcome!. You can see that ternary operator let us evaluate conditions in one line.
1.echo (date('G') < 12) ? 'Good Morning!' 'Welcome!';
We could also assign the value to a variable and print.
1.$greeting = (date('G') < 12) ? 'Good Morning!' 'Welcome!';
2.echo $greeting;
From PHP 5.3, it is possible to leave out the middle part of the ternary operator. In following example, if $name is not empty, its value will be printed. Otherwise Anonymous will be printed.
1.echo ($name) ? : 'Anonymous';

0 comments :