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:
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
|