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

Conditional Statements - "if" and "switch"

This chapter provides tutorial examples and notes about conditional statements. Topics include 4 forms of 'if' statements, 'switch' statements, examples of controlling code executions with conditional statements.

"if" Statements

"if" Statement Examples

"switch" Statements

"switch" Statement Examples

Conclusion:

  • "if" must be followed by a condition.
  • "elseif" is one word, no space.
  • "switch" must be followed by a test expression.
  • "default" clause will be executed, if no "case" clause matches the test expression.
  • Once a "case" is matched, all remaining statement blocks will be executed.
  • "break" statement should be used in "case" clauses to stop execution.

Dr. Herong Yang, updated in 2009
Conditional Statements - "if" and "switch"