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

Listen 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

  1. Cover of Web Database Applications with PHP and MySQL
    Web Database Applications with PHP and MySQL
    Print: $44.95
    Ebook: $35.99
  2. Cover of Essential PHP Security
    Essential PHP Security
    Print: $29.95
    Ebook: $23.99
  3. Cover of PHP Security Collection
    PHP Security Collection
    Ebook: $5.95
  4. Cover of Programming PHP
    Programming PHP
    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

©2009, 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