PHP DevCenter
oreilly.comSafari Books Online.Conferences.


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.

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: