PHP – Conditional Statements

The main purpose of conditional statements in PHP is to perform different kind of actions based on different conditions.

Conditional Statements in PHP

while we write the program we always needs to do different tasks based on different conditions. And for that purpose we use the following conditional statements.

  • if statement – “if statement” is used when execute a program if the condition returns true.
  • if…else statement – In this statement any of the one program will be executed. The program first check the “if condition” and will be executed when returns TRUE otherwise “else” statement will be executed.
  • if…elseif….else statement – This statement is used in more than two conditions.
  • switch statement – The “switch statement” is used to avoid long conditions of “if..elseif..else” code.

The if Statement in PHP

The “if statement” will be executed once the condition return TRUE results.

Syntax of if statement in PHP

if statement example

The if…else Statement in PHP

In this statement the “if program” will be executed once the condition return TRUE results otherwise the else program will be executed.

Syntax of if…else statement in PHP

if…else statement example

The if…elseif….else Statement in PHP

If there are multiple “if..else” conditions then we use The “if…elseif….else” Statement

Syntax of if…elseif statement in PHP

if…elseif statement example

The switch Statement in PHP

We will explain the switch statement in details in our next chapter so that you can understand it well. So please check our switch statement chapter for this.


Leave a Reply

Your email address will not be published.