Conditional statements in php

A conditional statement is a statement which performs or executes according to certain conditions set by the programmer. It is the set of rules performed if certain conditions are met.

There are 4 types of conditional statements available in php.

1.if 
2.if....else....
3.else...if ladder
4.switch


1.if statement

The if statement is a conditional statement that is used to check a condition. If the condition that was checked is true then the statements that given below the if statement in braces( {} ) is executed.

Syntax:

if(condition)
{
//Body of if
}



2.if...else.... statement

The if..else... statement is same as the if statement,but the if statement is followed by a else statement. If the conditions in the if statement is false, then the else block is executed.

Syntax:

if(condition)
{
//Body of if
}
else
{
//Body of else
}


3.if...elseif...else

This is also called else...if ladder because the if condition is followed by a number of elseif statement. And finally the else block is declared. The program will also work if the else statement is not given. If the first condition is false,then the second condition is executed. If the second condition is false ,then next block is executed. Finally if no condition are satisfied,then the else block is executed.

Syntax:

if(condition)
{
//Body of if
}
elseif(condition 1)
{
//Body of elseif
}
.
.
.
elseif(condition n)
{
//Body of elseif
}
else
{
//Body of else
}

 4.switch statement

The switch statement is the most efficient conditional statement since it executives only a certain block by jumping to that block by ignoring others. The switch statement consist of a number of cases. These cases are used to determine which block is to be executed. The condition in the switch is checked only one time at the beginning of the switch statement.

Syntax:

switch(n)
{
case label1://code to be executed
                      break;
case label2://code to be executed
                      break;
.
.
.
case labeln://code to be executed
                      break;
default://code to be executed;
}


Prev
Next

Comments

Popular posts from this blog

Node.js Cheat Sheet

Enhancing Supply Chain Security and Cloud-Native Development with Chainguard

Bootstrap ? What is it ?