12
Mar
We’ve gone through the basics for variables and assignments. With this section we’ll add some more operators and focus on control structures. Control structures alter or affect the programming flow. There are four main control structures we’ll cover: Conditionals, switches, loops, and functions. Loops and functions we’ll cover with the next session.
Conditionals
The most basic form of structure is the conditional or branching statement. This allows us to execute different statements depending on some condition. This type of statement also introduces a two new set of operators: conditional and logical operators. If you think of a logic structure there are a few ways to compare two values, as follows:
| equal to | $a == $b |
true if $a equal to $b; “0″ is equal to 0 | not equal to | $a != $b |
true if $a is not equal to $b |
|---|---|---|---|---|---|
| exactly equal to | $a === $b |
true if $a equal to $b and the type is the same; “0″ is not equal to 0 | Not identical to | $a !== $b |
true if $a is not equal to $b; “0″ is not equal to 0 |
| greater than | $a > $b |
true if $a is greater then $b | greater than or equal | $a >= $b |
true if $a is greater than or equal to $b |
| less than | $a < $b |
true if $a is less than $b | less than or equal | $a <= $b |
true if $a is less than or equal to $b |
| exclusive or | $a xor $b |
true only if one of $a and $b are true; but not both | not | ! $b |
true if $b is false |
| and | $a && $b |
true if $a and $b are both true. | or | $a || $b |
true if $a or $b are true. |
Basic Syntax
The most basic syntax is one that executes code only if something is true and is as follows:
if ( condition ) { statements; }
There are several things to note here. First, the statement starts with an “if”. Second, the condition must be contained within parenthesis. Third the code to be executed is contained within a “block”. Lastly, use white space and new lines liberally to assist the execution of coding. Blocks of code in PHP are always surrounded in braces, { … }. Below is a full sample:
if ($Wgt < $minWgt) { $Wgt = $minWgt; }
If…then…else
Often it’s not sufficient to simply execute code if something is true. Sometimes you need to execute code is something is true and different code if it’s not. In PHP, this is done with an “else” block. The basic syntax is as follows:
if ( condition ) { statements; }
else { statements; }
Both of these structure are typically executed with more than one statement and can often be found nested within each other, therefore balancing the curly braces can become a challenge if indenting and copious use of new lines is not followed. There are two main methods of handling this. In one, a newline is always entered before an open brace. All statements are then entered on their own lines and tabbed in. The closing brace is then outdented. In this manner, you can easily see which braces matchup. See the code below:
if ($theSpell->name==$spellName)
{
echo "Updating spell!";</code>
$theSpell->runes=$runes;
$theSpell->skillName=$skillName;
$theSpell->specName=$specName;
$theSpell->intensity=$int;
$theSpell->range=$range;
$theSpell->radius=$radius;
$theSpell->comments=$comments;
$theSpell->tn=$tn;
$theSpell->power=$power;
$theSpell->explain=$explain;
$unfound=false;
}
if ($unfound)
{
$theSpell=new spell($spellName, $runes, $skillName, $specName, $int, $range, $radius, '', $tn, $power, $explain);
$spellList[$skillName][$specName][]=$theSpell;
}
}
else
{
$theSpell=new spell($spellName, $runes, $skillName, $specName, $int, $range, $radius, '', $tn, $power, $explain);
$spellList[$skillName][$specName][]=$theSpell;
}
An alternate method keeps the opening brace on the same line as the if statement. The only real advantage of this conservation of one line. It’s the method I prefer, because text editors I use have braces and parenthesis balancing assistance and I don’t like to see several lines where the only thing on it is an opening brace. Consider the previous code with this style:
if ($theSpell->name==$spellName) {
echo "Updating spell!";</code>
$theSpell->runes=$runes;
$theSpell->skillName=$skillName;
$theSpell->specName=$specName;
$theSpell->intensity=$int;
$theSpell->range=$range;å
$theSpell->radius=$radius;
$theSpell->comments=$comments;
$theSpell->tn=$tn;
$theSpell->power=$power;
$theSpell->explain=$explain;
$unfound=false;
}
if ($unfound) {
$theSpell=new spell($spellName, $runes, $skillName, $specName, $int, $range, $radius, '', $tn, $power, $explain);
$spellList[$skillName][$specName][]=$theSpell;
}
}
else {
$theSpell=new spell($spellName, $runes, $skillName, $specName, $int, $range, $radius, '', $tn, $power, $explain);
$spellList[$skillName][$specName][]=$theSpell;
}
Assignment
For this assignment, I won’t give an example, but simply give you directions:
- Create a file called dieRoller.php
- Create three variables representing dice and give them the values, 1, 4, and 6
- Each die that has a value of 4 or greater should be counted as a success.
- Each 6 should have a new re-rolled die. This re-roll will be 3.
- Have the program calculate the number of successes of this roll (hint to add 1 to a variable use the following: $a = $a + 1;
- Output to the user the values of all dice, identifying the re-roll, and the number of successes
