3
<?
$a=1; #Sets value of $a to 1
$b=3; #Sets $b to 2
$c=$a+$b; #Sets $c to the sum of $a and $b
$d=$a-$b; #Subtracts $b from $a and stores it in $d
$e=$a*$b; #$e becomes $a multiplied by $b
$f=$a/$b; #Sets $f to $a divided by $b
$a++; #Increments the value of $a
$b--; #Subtracts 1 from the value of $b
$a+=$b; #sets $a = $a + $b
$a-=$b; #sets $a = $a - $b
$b*=$c; #sets $b = $b * $c
$b/=$c; #sets $b = $b / $c
?>
If...else statements are used to determine true or false. If the statement is true do this else do that.
<?
$a=2;
if ($a<3) {
echo "True";
} else {
echo "false";
}
?>
The above will echo True because two is less than three. If $a is changed to a number three or higher then false will be displayed.
Along with if and else this control structure has another element, elseif. elseif can be placed in the control structure to give more than two possible options. So if $a is negative do one thing if $a is 0 do another and if $a is positive do a third thing.
<?
if ($a < 0) {
echo "$a is negative";
} elseif ($a == 0) {
echo "$a is 0";
} elseif ($a == 1) {
echo "$a is 1";
} elseif ($a > 1) {
echo "$a is greater than 1";
}
?>
elseif can be chained together for as many conditions as you like. If you are going to be using this to filter the value of one variable use the switch...case statements discussed on the next page.