Understanding Decision Structures
Published: January 22nd, 2009 by: Kurtis
Decision structures form the basis of all programming languages; just as they sound, these constructs move the program down a certain path depending on variables which are checked. This gives a program functionality and the ability to handle different input and analyze the data within the script.
The comparision operators are:
== | ($a == $b) | Returns true if $a equals $b |
=== | ($a === $b) | Returns true if $a equals $b and is of the same type (int, string, etc) |
!= | ($a != $b) | Returns true if $a does not equal $b |
!== | ($a !== $b) | Returns true if $a does not equal $b or is not of the same type |
< | ($a < $b) | Returns true if $a is less than $b |
> | ($a > $b) | Returns true if $a is greater than $b |
<= | ($a <= $b) | Returns true if $a is less than or equal to $b |
>= | ($a >= $b) | Returns true if $a is greater than or equal to $b |
Logical operators are a bit different but are also used in decision structures. There are five different logical operators, but for the sake of this article, in truth three are the most important.
&& , and | ($a && $b) | Returns true if A and B are both be true |
|| , or | ($a || $b) | Returns true if A or B (or both) is (are) true |
xor | ($a xor $b) | Returns true if A or B is true and the other is false |
For more information on the reason behind two variations of “and” and “or” operators, check out the manual on operator precedence.
Now let’s get on to the decision-making part of programming with PHP. There are two main ways to do this, and we will start with the if-else statements.
Basically, this comprises the main idea of decision making. It handles like this: “If THIS is true, do something. Otherwise (else), do something else.” Using the operators defined above, you can make a basic script:
if ($a == $b) { echo "The variables are equal."; } else { echo "The variables are unequal."; } ?>
This would be great if there were always just two choices, but sometimes there are several choices between these two. Think about a school grading scale. The following would assign a grade based on a ten-point scale. Since there are more than two possibilities, we will add some elseif statements inbetween.
<?php $grade = 93; if ($grade >= 90) { $letter-grade = "A"; } elseif ($grade > 80) { $letter-grade = "B"; } elseif ($grade > 70) { $letter-grade = "C"; } elseif ($grade > 60) { $letter-grade = "D"; } else { $letter-grade = "F"; } ?>
Notice that we work downward to make sure the grade only fits in one category. It’s important to think about your script logically before setting out to write it. Now, the type of structure described above will handle any situation, but in truth it isn’t always the best choice. Suppose you wanted to create a script that would change a certain heading based on the date, if it is a holiday or not. While an if-else/elseif structure would work, there is an alternative, the “switch” construct which focuses on just one variable rather than comparison between two or more.
<?php $date = ...; //Obtain date in the form "mm/dd" switch($date) { case "01/01": echo "Happy New Year"; break; case "02/14": echo "Happy Valentine's Day"; break; case "05/10": echo "Happy Mother's Day"; break; default: echo "Happy Day!"; break; } ?>
Obviously, hundreds of other holidays can be added in as a different “case,” but the idea is captured with this short script. The “default” case acts as an “else” in this structure, catching anything that did not match any case.
With these two ideas, it is easy to build fairly impressive scripts with PHP, and you will even find that the idea carries over to most different programming languages with tiny changes in syntax and operators. Combined with more complex variables handled by user input and organized sessions, you will see that decision structures are truly the core of a programming language.