Now that you've got the basics of PHP variables and operators down, the second article in this series takes a look at PHP's form-processing capabilities, and introduces you to the comparison and logical operators and the "if-else" and "switch" family of conditional statements.
PHP also provides you with a way of handling multiple possibilities - the "if-elseif-else" construct. A typical "if-elseif-else" statement block would look like this:
if (first condition is true)
{
do this!
}
elseif (second condition is true)
{
do this!
}
elseif (third condition is true)
{
do this!
}
... and so on ...
else
{
do this!
}
And here's an example that demonstrates how to use
it:
As you can see, this is simply a form which allows you to
pick a day of the week. The real work is done by the PHP script "cookie.php4"
<?
if ($day == "Monday")
{
$fortune = "Never make anything simple and efficient when a way can
be found to make it complex and wonderful.";
}
elseif ($day == "Tuesday")
{
$fortune = "Life is a game of bridge -- and you've just been
finessed.";
}
elseif ($day == "Wednesday")
{
$fortune = "What sane person could live in this world and not be
crazy?";
}
elseif ($day == "Thursday")
{
$fortune = "Don't get mad, get interest.";
}
elseif ($day == "Friday")
{
$fortune = "Just go with the flow control, roll with the crunches,
and, when you get a prompt, type like hell.";
}
else
{
$fortune = "Sorry, closed on the weekend";
}
?>
<html>
<head>
<basefont face="Arial">
</head>
<body>
Here is your fortune for <? echo $day; ?>:
<br>
<b><? echo $fortune; ?></b>
</body>
</html>
In this case, we've used the "if-elseif-else" control
structure to assign a different fortune to each day.
There's one important point to be noted here - as soon as one of the "if" statements within the block is found to be true, PHP will execute the corresponding code, skip the remaining "if" statements in the block, and jump immediately to the lines following the entire "if-elseif-else" block.{mospagebreak title=A Little Bit Of Logic} Now, you've already seen that PHP allows you to nest conditional statements. However, if you take another look at the example we used to demonstrate the concept
<?
if ($day == "Thursday")
{
if ($time == "12")
{
if ($place == "Italy")
{
$lunch = "pasta";
}
}
}
?>
you'll agree that is both complex and frightening. And so, in
addition to the comparison operators we've used so liberally thus far, PHP also provides a few logical operators which allow you to group conditional expressions together. The following table should make this clearer.
Operator
What It Means
Example
Evaluates To
&&
AND
$delta == $gamma && $delta > $omega
True
$delta && $omega < $omega
False
||
OR
$delta == $gamma || $delta < $omega
True
$delta > $gamma || $delta < $omega
False
!
NOT
!$delta
False
<=
is less than or equal to
$delta <= $omega
False
Given this knowledge, it's a simple matter to rewrite the example above in terms of logical operators:
Simple and elegant? Yes.{mospagebreak title=Switching Things
Around} An alternative to the "if-else" family of control structures is PHP's "switch" statement, which does almost the same thing. It looks like this
switch (decision-variable)
{
case first_condition_is true:
do this!
case second_condition_is true:
do this!
case third_condition_is true:
do this!
... and so on...
}
We'll make this a little clearer by re-writing our fortune
cookie example in terms of the "switch" statement.
[cookie.php4]
<?
// the decision variable here is the day chosen by the user
switch ($day)
{
// first case
case "Monday":
$fortune = "Never make anything simple and efficient when a way can
be found to make it complex and wonderful.";
break;
// second case
case "Tuesday":
$fortune = "Life is a game of bridge -- and you've just been
finessed.";
break;
case "Wednesday":
$fortune = "What sane person could live in this world and not be
crazy?";
break;
case "Thursday":
$fortune = "Don't get mad, get interest.";
break;
case "Friday":
$fortune = "Just go with the flow control, roll with the crunches,
and, when you get a prompt, type like hell.";
break;
// if none of them match...
default:
$fortune = "Sorry, closed on the weekend";
break;
}
?>
<html>
<head>
<basefont face="Arial">
</head>
<body>
Here is your fortune for <? echo $day; ?>:
<br>
<b><? echo $fortune; ?></b>
</body>
</html>
There are a couple of important keywords here: the "break"
keyword is used to break out of the "switch" statement block and move immediately to the lines following it, while the "default" keyword is used to execute a default set of statements when the variable passed to "switch" does not satisfy any of the conditions listed within the block.