PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


PHP Foundations

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.

Let's take a look at an example:

<?php
  $foo = 10;
  $bar = 12;
  if($foo > $bar) {
    echo "$foo is greater than $bar";
  } else {
    if($foo == $bar) {
    echo "$foo is equal to $bar";
    } else {
    echo "$foo is less than $bar";
    }
  }
?>

By now I would hope that the above code is easy to understand. Basically all we are doing is showing the relationship between two integers $foo and $bar and outputting that

Related Reading

PHP Pocket ReferencePHP Pocket Reference
By Rasmus Lerdorf
Table of Contents
Sample Section
Full Description
Read Online -- Safari

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:

If(<conditon>) {
  ...
} [else[if(<condition>)] {
  ...
} [else {
  ...
} ] ]

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:

<?php
  $foo = 10;
  $bar = 12;
  if($foo > $bar) {
    echo "$foo is greater than $bar";
  } elseif($foo == $bar) {
    echo "$foo is equal to $bar";
  } else {
    echo "$foo is less than $bar";
  }
?>

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

Consider the following conditional if statement:

<?php
  $foo = 5;
  $bar = 10;
  if($foo > $bar) {
   $foobar = true;
  } else {
   $foobar = false;
  }
?>

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:

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

Where $foobar will only be assigned "true" if $foo is greater than $bar, and $boolean has a value of "true." A better way to write such a conditonal would be:

<?php $foobar = (($foo > $bar) && $boolean) ? true : false; ?>

Or use the standard if conditional structure.

Pages: 1, 2

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: