PHP DevCenter

oreilly.comSafari Books Online.Conferences.

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

Search
Search Tips

advertisement

Print Subscribe to PHP Subscribe to Newsletters

Advanced Control Structures
Pages: 1, 2

Multi-conditional 'if' statements

In programming, it is often necessary to execute a piece of code based on more than one condition. To accomplish this, PHP provides a few ways to evaluate a code block based on multiple conditions. Although the desired result can be achieved by simply embedding if statements within other if statements, this is often an excessive and confusing way to accomplish the desired effect. To help remedy this problem, special conditional operators "and" (&&) and "or" (||) are provided. These operators separate multiple conditions within an if statement and determine the final outcome of an evaluation. For example, consider the following statement:



If Billy has $5, and his mom says it’s okay, he can have a candy bar.

We already learned how to code this conditional statement by embedding two if statements as shown below:

If($dollars >= 5) {
If($mom_okay) {
// Code when both conditions are true
}
}

We see that in order for Billy to have a candy bar, two conditions must be met: He must have money ($5 minimum) and his mother must agree. In PHP, such a statement could also be written as follows:

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

If( ($dollars >= 5) && ($mom_okay) ) {
// Code if both conditions are true
}

As you can see, we have used the "and" operator to combine the original two separate conditional statements into a single conditional statement.

Note: The use of parentheses in the above example is to ensure that each separate conditional statement is evaluated properly before the results of those conditions are evaluated through the "and" operator.

If all conditions that are combined using the "and" operator must evaluate to "true" for the statement to be true, then when using the "or" operator only a single condition of the combined conditions must evaluate to "true" in order for the statement to be true. For example, if we modified our example to read:

If Billy has $5 or if his mom agrees, he can have a candy bar.

Using basic if statements, our code would resemble the following:

If($dollars >= 5) {
// Code if condition is true
}

If($mom_okay) {
// Identical to code in the first conditional
}

In this example, the complexity and inefficiency of our code increases because we have to duplicate code twice. To make this code more efficient, we can combine these two conditional statements using our new conditional operators.

The same statement using these operators would look something like this:

If( ($dollars >= 5) || ($mom_okay) ) {
// Code if either condition is true
}

This multi-conditional syntax is not limited to if statements and can be used any place where a conditional statement is required (including while and for loops).

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.


Read more PHP Foundations columns.

Return to the PHP DevCenter.




Recommended for You

Tagged Articles

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

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • Partner Sites
  • makezine.com
  • makerfaire.com
  • craftzine.com
  • igniteshow.com
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com