PHP if else

Conditional statements are used when we want to perform an action based on a condition. For instance, I can check if it's raining to decide whether to stay home or go out.

Conditional statements have a body structure (wrapped in between curly braces {} ). PHP keywords for condional statements are if, else, else if.


Decisions in PHP take the following form:

The else part of the above is optional. A condition is an expression that can be evaluated to true or false. PHP treats 0 and the empty string as false, and everything else as true. Here is some example code:

IF statements can be chained together using ELSEIF :


An Example:

Click here to read about switch statements.