PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to PHP Subscribe to Newsletters
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




Recommended for You

  1. Cover of Head First PHP & MySQL
    Head First PHP & MySQL
    Print: $44.99
    Ebook: $35.99
  2. Cover of Web Database Applications with PHP and MySQL
    Web Database Applications with PHP and MySQL
    Print: $44.95
    Ebook: $35.99
  3. Cover of PHP Pocket Reference
    PHP Pocket Reference
    Print: $9.95
  4. Cover of PHP Anthology
    PHP Anthology
    Print: $39.95

Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2010, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com