Understanding Decision Structures

Published: January 22nd, 2009 by: 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.

Though the term sounds complex, decision structures just help the script make a decision.  If something is true/false, do this.  If not, do this.  All decision structures come down to a true or false situation, and usually a comparison is made that results in one of the two choices.  There are several comparison operators, and it takes a basic understanding of logic to fully utilize decision structures.

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

}
}
}
}
else {
}
?>

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.

Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.