Control Structures Revisited

08/23/2001

Today we'll revisit our discussion of control structures with the introduction of three new ones: do...while(), if....elseif, and the switch statement.

Last time we discussed control structures we introduced the standard if() statement, the while() statement as well as the for() statement. Today we'll introduce two new variations of these control structures as well as introduce a completely new structure switch() and explain the similarities between it and the other structures.

The 'if' statement revisited

The if statement was the first, and most important, control structure we learned. We learned how to use it to create conditional-executing code and how we could use its companion, the else statement, to guarantee that one part or the other would always be executed. Today we'll introduce briefly a new type of if structure used when multiple conditional checks are needed as well as a rather strange-looking conditional structure called the "conditional assignment" operator.

relationship to the browser. Although effective, the above set of control structures is very messy and could easily become a nightmare. It is for this reason that PHP has provided us with a different type of else statement called elseif. Its use is identical to a normal if statement except instead of using the else statement we substitute the elseif statement and provide another conditional. The formal syntax for the statement is as follows:

When used in practice its primary purpose is to clean up our scripts and make them easier to understand rather than providing any real extra benefits. For an example, we'll rewrite our relationship example to use the new elseif statement:

This script will function identically to our original version with the added bonus of removing the need to embed a second if statement within the else of another. There is no limit to how many elseif statements can be "chained" together within a single if conditional statement but any more than two and it is recommended that the switch() statement (that we'll cover later in today's article) be used.

The conditional assignment operator

Often times it is necessary for a developer to assign the value of a variable depending on the evaluation of a conditional between two others. In this case, we are assigning a boolean value of true or false to the variable $foobar, depending of course on the outcome of the conditional $foo > $bar. As with our first example during our discussion of the elseif statement, the above will work as expected but is rather sloppy.

Today we'll introduce a new type of syntax available in PHP that acts both as a conditional control structure and as an operator all at the same time. This control structure is called (for obvious reasons) the conditional assignment operator. The syntax for this new operator/control structure is as follows:

$var = (<condition>) ? <true value> : <false value>;

Although it looks somewhat strange, it functions exactly as the example just provided. The variable $var is assigned a value dependent on the conditions. If the conditions are "true," the true value will be assigned to $var and conversely if "false," the false value will be assigned. For a better illustration of this, we'll rewrite our example to use this new syntax:

<?php
$foo = 5;
$bar = 10;
$foobar = ($foo > $bar) ? true : false;
?>

As we stated, the above example functions exactly as our previous one and will produce the same result. It is strongly recommended that a conditional assignment operator is used instead of an if statement when attempting to assign a variable either one variable or another. However, for the sake of readability this operator should not be used in the following manner: