if, elseif, and else control structures
When using a programming language there is most often a need to do different tasks based on different situations. The method used to do this sometimes varies, but in PHP it is quite easy to do.
We use the if, elseif, and else control structures for this. Here is an example of how if/elseif/else statements come together:
<?php
|
|
I'll go through this step-by-step to explain what is going on. First off, the code starts with an if statement. This tells PHP to evaluate the expressions inside the parenthesis and if it evaluates to TRUE, then PHP executes the lines of code within the curly braces that immediately follow. I say expressions because you can have virtually any number of conditions for each if statment (more on this later).
Next, if the first if statement returns FALSE then PHP moves onto the elseif statement. If does the same thing here that it did with the initial if statement. If the expressions inside of the parenthesis evaluate to TRUE it performs the lines inside of the curly braces that immediately follow.
Finally, if both the if and the elseif statements have failed, PHP defaults to the else statement. Notice that the else does not have conditions to follow. PHP simply executes the code in the curly braces.
This is the basic structure for any if/elseif/else statement. Using them to your advantage mostly involves the operators you use with your conditional tests.
Conditional and Logical operatorsMore often then not, conditional statments will use one or more operators to complete the conditional test. The kind of operators we'll use for our conditional tests are comparison operators and logical operators. For more information on operators visit the PHP manual.
Let's try out some basic if statements.
<?php
|
|
These are rather vague examples, but their purpose is to illustrate the different structures of conditional statements as well as to introduce some of the operators that can be used within the conditional expressions.
If you take a look at the second if/elseif/else statement, lines 23-27, you will notice that it does not contain an elseif expression. When using conditional statements you do not have to use each part. However, you must have the if portion.
The operator used in the second if/elseif/else statement (==) is the comparison operator "Equal". It simply compares the two values and returns TRUE or FALSE depending on those values. There are many other comparison operators that you may use, including "Less than" (<), "Greater than" (>), "Less than or equal to" (≤), "Greater than or equal to" (≥) and so forth. For a complete list of comparison operators, have a look at the PHP manual.
The final if/elseif/else statement, lines 31-38, is basically the same as the previous. The only difference here is the addition of the elseif portion and the logical operator for "AND" (&&).
The "AND" operator allows you to include more conditions that the entire expression must have in order for it to return TRUE or FALSE. There is also a logical operator for "OR" (||). It is used in the same way that "AND" is used, but has the opposite effect. For a complete list of logical operators, have a look at the PHP manual.
That's pretty much it for conditionals. They're pretty easy to use, you just have to try them out yourself and play with the different operators. If you have any questions, feel free to discuss this tutorial below.
| Discuss Tutorial: if, elseif, else | 1 Comment |
