PHP Conditional Statements
Conditional statement in php used to take decision on based the conditions is true or false.
There are basic four conditional statements in PHP.
if statement
if – else statement
if – elseif – else statement
switch statement
if statement
if condition – this statement is used when we want to execute a code only one condition must be true.
if condition syntax :
<?php
if (condition)
{
// condition is true then code to be executed
}?>
if – else statement
if-else condition – this statement is used when we want to execute a code in both the condition. if condition is true execute some code, if false execute another code.
if-else condition syntax:
<?php
if(condition)
{
// condition is true
}
else
{
// condition is false
}
?>
if – elseif – else statement
if-elseif-else statement – this statement is used when we want to execute a code if one of multiple condition is true.
if-elseif-else statement syntax:
<?php
if (condition)
{
// if this condition is true then code to be executed
}
else if (condition)
{
// if this condition is true then code to be executed
}
else if (Condition)
{
// if this condition is true then code to be executed
}
else
{
// all conditions are false then this code to be executed
}
?>
switch statement
switch statement – same as if-elseif-else this statement is used we have multiple condition but only one condition true that code will be executed.
switch statement syntax:
<?php
switch (a)
{case statement1:
if a=statement1 then code to be executed;
break;
case statement2:
if a=statement2 then code to be executed;
break;
case statement3:
if a=statement3 then code to be executed;
break;
…….
default:
if “a” is not matches with any statement then code to be executed.
}
?>
Next tutorials we will learn all php statements one by one with an example.