PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

"if" Statements

This section describes 4 forms of 'if' statements supported in PHP. The more complex form is 'if ... elseif ... else'.

There are 4 forms of "If" statements supported in PHP:

1. Single-statement "if":

   if (condition) statement

where "condition" is a Boolean value, and "statement" specify another statement. The specified statement will be executed, if and only if the specified condition is evaluated to "TRUE".

2. Multi-statement "if":

   if (condition) {
      statement;
      statement;
      ...
   }

The specified multi-statement block will be executed, if and only if the specified condition is evaluated to "TRUE".

3. "if ... else" Statement:

   if (condition) {
      statement;
      statement;
      ...
   } else {
      statement;
      statement;
      ...
   }

Two statement blocks are specified. But only one statement block will be executed based on the value of the specified condition. If the specified condition is "TRUE", the first block will be executed. If the specified condition is "FALSE", the second block will be executed.

4. "if ... elseif" Statement:

   if (condition_1) {
      statement;
      statement;
      ...
   } elseif (condition_2) {
      statement;
      statement;
      ...
   } elseif (condition_3) {
      statement;
      statement;
      ...
   ...
   } else {
      statement;
      statement;
      ...
   }

Many statement blocks are specified. But only one statement block will be executed based on the value of the condition specified for that block.

Last update: 2005.

Sections in This Chapter

"if" Statements

"if" Statement Examples

"switch" Statements

"switch" Statement Examples

Dr. Herong Yang, updated in 2009
"if" Statements