Conditionals

Main | Static vs. Dynamic Websites | Using PHP | A Simple PHP Program | Variables and Operators | Practice 1 | Solution 1 | Loops | Practice 2 | Solution 2 | Functions | Practice 3 | Solution 3 | Conditionals | Practice 4 | Solution 4

In our lives we often plan what we will do based on conditions. Statements like "if it is before 8:30, then I have time for breakfast", is a good example of this. In making such a statement, the speaker sets a rule that he can always follow, because it is based on a condition that he can check later. PHP programs are the same way, since they must often respond to a particular input in an appropriate way, and a conditional statement (e.g, if, then, else) is the easiest way to implement this behavior.

In PHP conditional statements are similar to English. We usually declare if some condition then the action that we will take. In PHP such a statement would look like the following:

if (some condition) {
        the action that we will take
}

Note in the above that stating then is implicit, and is not necessary (nor supported) in PHP. The if statement must be followed by a set of parentheses, and inside of those parentheses must be a condition that can be checked to see if it is true or false. Such a condition is usually a statement which checks if one number is less than another. For example, (100 < 1) is false, and (100 > 1) is true. If the condition within the parentheses is true, then whatever PHP statements there are that are inside of the curly brackets ({ and }) will be executed. Conversely, if the condition within the parentheses is false, then the PHP statements that are inside of the curly brackets will not be executed, and PHP will move onto the statement that follows the closing curly bracket (}), and your program will continue from there.

Let's examine a PHP program that uses conditionals. Recall our first PHP example Date & Time which used the built-in PHP function date(). The following statement creates a variable ($var), which can take on any integer value between 0 and 23, corresponding to an hour from a 24-hour day (0 is Midnight, 1 is 1 AM, ... , 23 is 11 PM)).

$var = date("H");
The variable above will take on only the value of the hour of the day (not the minutes, nor the seconds). Once we have $var declared, we can use it along with conditionals, to say "good morning", "good afternoon", or "good evening" , as in the PHP program below.
<html>
<?
$var = date("H");

if ($var <= 11) {
        echo "good morning";
}
else {
        if ($var > 11 and $var < 18) {
                echo "good afternoon";
        }
        else {
                echo "good evening";
        }
}
?>
</html> 
You can run the program above by clicking here. The above also uses the conditional statement else. You can only use an else statement after an if statement. An else statement has curly brackets, the same way that an if statement does, and is only executed if the condition within the parentheses of the if statement is false. Thus, if the above statement ($var <= 11) was true, "good morning" would appear on your browser, and PHP would not execute the rest of the program. However, if ($var <= 11) was false, PHP would execute only the statements inside of the first else statement.

There are four other crucial aspects of conditionals that must be discussed:

  1. You are welcome to nest conditionals inside of other conditionals, as in the example above, which opens a new if statement inside of an else statement.

  2. You can use the statement and to test the value of more than one condition. If you use an and, all of the conditions that you combine inside of the parentheses have to be true for the statement that follows to be executed. Suppose that you have the following code:
    if (statementA and statementB) {
            something();
    }
    
    The function something(); will only be executed if both statement A and statement B are true. To give a more formal account of and we will do the following:

    • To denote that a statement is true or false, we will label it with either a T or an F.

    • We will then make a table of all of the combinations in which the statements A and B can be true or false. The table will have four rows, because:
      1. Both A and B can be true
      2. A can be true and B can be false
      3. A can be false and B can be true
      4. Both A and B can be false

    • We will then make a column in our table containing the truth-value of A and B. Our table would be the following:

      row statement A statement B A and B
      1 T T T
      2 T F F
      3 F T F
      4 F F F

    This table, more formally known as a truth-table relates to the code above, in that row 1 will be the only case in which the function something(); will be executed, because that is the only case in which both A and B are true.

  3. You can use the statement or to test the value of more than one condition. If you use an or, only one (as opposed to all, in the case of and) of the conditions inside of the parentheses has to be true for the statement inside to be executed. Suppose we had the following code:
    if (statementA or statementB) {
            something();
    }
    

    The function something(); will only be executed when statements A or B are true. A truth-table can be constructed for or as well as and. The table below relates to the code above in that the function something(); will only be executed in the cases demonstrated by rows 1, 2, and 3.

    row statement A statement B A or B
    1 T T T
    2 T F T
    3 F T T
    4 F F F

  4. You can always test the opposite of a condition by using the not operator, represented in PHP with an exclamation point (!). Suppose we had the following code:
    if (!statementA) {
             something();
    }
    
    In the above code, something(); will only be executed if statementA is false.

A rigorous understanding of conditionals will give us the control we need when programming PHP. Practice 4 will emphasize conditionals and Practice 5 show how they can be used to make online forms. Practice 6 will then apply them to make a common and practical web application.


jfulton [at] member.fsf.org
22 Aug 2013