CodingBison

More often than not, programs execute different set of statements depending upon a specified condition. For example, if there was a program looking for user details of "Homer Simpson" in the list of available users and if it were to find "Homer Simpson", then it can indicate so. Else, it should continue its search.

PHP provides conditional expressions for executing different tasks based upon the value of a conditional variable or an expression. There are four types of conditional expressions: (a) if, (b) if-else, (c) if-elseif-else, and (d) switch. We can consider the first two forms as simplification of the if-elseif-else form.

Expressions: if, if-else, and if-elseif-else

We provide below generic formats for the if, if-else, and if-elseif-else expressions. For the if-elseif-else clause, there is no limit to the occurrence of elseif clause. We show two conditions ("condition1" and "condition2") with the elseif clause; however, a program can have as many number of such clauses as it requires.

 /* Format of a single if expression */
 if (condition) {
     Execute some statements
 } 

 /* Format of an if-else expression */
 if (condition) {
     Execute statements pertaining to the "condition"
 } else {
     Execute Alternative statements
 }

 /* Format of an if-elseif-else expression */
 if (condition0) {
     Execute statements pertaining to the "condition0"
 elseif (condition1) {
     Execute statements pertaining to the "condition1"
 elseif (condition2) {
     Execute statements pertaining to the "condition2"
 } else {
     Execute Alternative statements
 }

We provide a simple program to implement these conditional expressions. Next, we present below a program that uses these expressions to print certain records from an array. In real life applications, these user details would perhaps be stored in a proper data handling infrastructure like a database! However, the focus here is on using these conditional expressions and so, we keep the data storage part trivial.

 <?php
 /* Names of users who have signed up for the application */
 $username_array[0] = "James Fulton";
 $username_array[1] = "Thomas Kipling";
 $username_array[2] = "Lisa Simpson";
 $username_array[4] = "Marge Simpson";
 $username_array[5] = "Homer Simpson";
 $username_array[6] = "Lilly Sellers";

 /* An if clause */
 foreach ($username_array as $element) {
     if ($element == "Homer Simpson") {
         echo "[if] Woohoo!! Found a record for Homer! <br><br>";
         break;
     }
 }

 /* An if-else clause */
 foreach ($username_array as $element) {
     if ($element == "Homer Simpson") {
         echo "[if-else] Woohoo!! Found a record for Homer! <br>";
         break;
     } else {
         echo "[if-else] This record is NOT for Homer.<br>";
     }
 }

 /* An if-elseif-else clause */
 echo "<br>";
 foreach ($username_array as $element) {
     if ($element == "Homer Simpson") {
         echo "[if-elseif-else] Found a record for a Simpson: $element <br>";
     } elseif  ($element == "Marge Simpson") {
         echo "[if-elseif-else] Found a record for a Simpson: $element <br>";
     } elseif  ($element == "Lisa Simpson") {
         echo "[if-elseif-else] Found a record for a Simpson: $element <br>";
     } else {
         echo "[if-elseif-else] This record is NOT for Simpsons <br>";
     }
 }
 ?>

And, here is the output of the above program, when we load the above example on a browser:

 [if] Woohoo!! Found a record for Homer!

 [if-else] This record is NOT for Homer.
 [if-else] This record is NOT for Homer.
 [if-else] This record is NOT for Homer.
 [if-else] This record is NOT for Homer.
 [if-else] Woohoo!! Found a record for Homer!

 [if-elseif-else] This record is NOT for Simpsons
 [if-elseif-else] This record is NOT for Simpsons
 [if-elseif-else] Found a record for a Simpson: Lisa Simpson
 [if-elseif-else] Found a record for a Simpson: Marge Simpson
 [if-elseif-else] Found a record for a Simpson: Homer Simpson
 [if-elseif-else] This record is NOT for Simpsons 

Yet another form of conditional expression is provided in the ternary form of "if (condition) ? do_this_1 : do_this2". This means that if the "condition" is true, then we execute "do_this_1", else, we execute "do_this_2".

A simple example of this handy expression could be to find the maximum of the two numbers. We provide the example below. The output of the example is "The maximum of these two numbers is 10".

 <?php
 $number1 = 5;
 $number2 = 10; 

 $maximum = ($number1 > $number2 ? $number1 : $number2);

 echo "The maximum of these two numbers is $maximum";
 ?>

Switch Expression

For certain cases, PHP provides a more compact version of an if-elseif-else condition: switch. If an if-elseif-else clause has conditions that belong to a set of values, then we can use the switch expression for such cases. This variant allows a program to check if the value of a passed expression (or value) belongs to the set of values.

We show a typical format of the switch statement in the following pseudo-code; the values ($value0, $value1, ...) do not necessarily have to be integer constants and can be variables or expressions.

 switch (expression) {
     case $value0:
     Execute statements pertaining to the "value0"
     break;

     case $value1:
     Execute statements pertaining to the "value1"
     break;

     default:
     Execute Alternative statements
     break;
 } 

A switch works by comparing the passed expression (or value) to different values from the set ($value0, $value1, ...). The switch statement begins by comparing the passed expression with the first value from the set (or the first case). If the comparison succeeds, then it breaks, otherwise, it falls to the next value from the set. This way, it continues to compare with each value till it finds a match.

Worst case, if the passed value does not match any of the switch cases, then it hits the "default" case and exits the switch. Please note that we must provide a break statement at the end of a given case, if we do not want the switch to do any further comparison.

We rewrite the earlier if-elseif-else program to use a switch statement. The three case statements check names of three users and if the passed username belongs to the set of these three names, then it prints the record.

In fact, we could also make several case statements do the same task. In this case, since we are merely printing the record for each name, we can let the case statements fall through for each of the names and then print the record at the end. We intentionally remove the break statements to make the evaluation fall through all the cases.

Here is the program:

 <?php
 /* Names of users who have signed up for the application */
 $username_array[0] = "James Fulton";
 $username_array[1] = "Thomas Kipling";
 $username_array[2] = "Lisa Simpson";
 $username_array[4] = "Marge Simpson";
 $username_array[5] = "Homer Simpson";
 $username_array[6] = "Lilly Sellers";

 /* Let us use a switch clause */
 foreach ($username_array as $element) {
     switch ($element) {
     case "Marge Simpson": 
         echo "[switch] Found a record for a Simpson: $element <br>";
         break;
     case "Homer Simpson": 
         echo "[switch] Found a record for a Simpson: $element <br>";
         break;
     case "Lisa Simpson": 
         echo "[switch] Found a record for a Simpson: $element <br>";
         break;
     default: 
         echo "[switch] This record is NOT for Simpsons <br>";
         break;
     }
 }

 /* Let us use a switch clause with Fall-through */
 echo "<br>";
 foreach ($username_array as $element) {
     switch ($element) {
     case "Marge Simpson": 
     /* Fall through */
     case "Homer Simpson": 
     /* Fall through */
     case "Lisa Simpson": 
         echo "[switch] Found a record for a Simpson: $element <br>";
         break;
     default: 
         echo "[switch] This record is NOT for Simpsons <br>";
         break;
     }
 }
 ?>

Here is the output:

 [switch] This record is NOT for Simpsons
 [switch] This record is NOT for Simpsons
 [switch] Found a record for a Simpson: Lisa Simpson
 [switch] Found a record for a Simpson: Marge Simpson
 [switch] Found a record for a Simpson: Homer Simpson
 [switch] This record is NOT for Simpsons

 [switch] This record is NOT for Simpsons
 [switch] This record is NOT for Simpsons
 [switch] Found a record for a Simpson: Lisa Simpson
 [switch] Found a record for a Simpson: Marge Simpson
 [switch] Found a record for a Simpson: Homer Simpson
 [switch] This record is NOT for Simpsons

It is a good practice to provide the comment "/* Fall through */" -- this way, a new programmer who gets to enhance/maintain this code would know that the fall-through has been intentionally put as per the design.





comments powered by Disqus