PHP if, if else and if elseif else Statement

PHP if, if else and if elseif else Statement

In this chapter, you will learn PHP if, if else and if elseif else Statement with Example.

All the above PHP statements are the conditional statements.

What are the Conditional Statements?

The conditional statements are used to perform or execute a different block of actions or instructions based on different conditions.


Table of contents:

  • if Statement
  • else Statement
  • elseif Statement

PHP if Statement

if statement in PHP

हिंदी | PHP if Statement in Hindi

The PHP if statement executes a block of code when the specified condition is true.

Syntax of if statement

<?php
//condition inside the parentheses

if(condition goes here){
  //code to be executed, if the condition is true
}
?>

Example:

<?php
if(true){
  echo "Condition is true";
}
if(false){
  echo "Condition is false";
}
?>
Browser Output
Condition is true

<?php
//return true
var_dump(2 + 4 == 6);
echo "<br>";

//return false
var_dump(2 + 4 == 8);
echo "<br>";

//using with if statement

//true Condition example
if(2 + 4 == 6){
  echo "1 - Condition is true";
}
// false Condition example
if(2 + 4 == 8){
  echo "2 - Condition is true";
}
?>
Browser Output
bool(true)
bool(false)
1 – Condition is true

PHP else Statement

If else statement in PHP

हिंदी | PHP If Else Statement in Hindi

The PHP else statement executes a block of code when the specified condition is false.

Syntax of else statement

<?php
if(condition goes here){
  //code to be executed, if the condition is true
}
else
{
  //code to be executed, if the condition is false
}
?>

Example:

<?php
//true Condition example
if(2 + 4 == 6){
  echo "1 - Condition is true";
}
else{
  echo "1 - Condition is false";
}

echo "<br>";

// false Condition example
if(2 + 4 == 8){
  echo "2 - Condition is true";
}
else{
  echo "2 - Condition is false";
}
?>
Browser Output
1 – Condition is true
2 – Condition is false

PHP elseif Statement

else if Statement in PHP

हिंदी | PHP elseif Statement in Hindi


The elseif statement is a combination of if and else statements. The elseif statement executes a block of code when the condition is true, and all the above conditions are false.

Basically, the elseif statement is used to executes different codes for more than one conditions.

Syntax of elseif Statement

<?php
if(first condition){
 //code to be executed, if the first condition is true
}
elseif(second condition){
  //code to be executed, if the second condition is true, and all the above conditions are false.
}
else{
  //code to be executed, if all the above conditions are false
}
?>

Example:

<?php
$a = 5;
$b = 5;

if ($a > $b) {
    echo "A is bigger than B";
} elseif ($a == $b) {
    echo "A is equal to B";
} else {
    echo "A is smaller than B";
}
?>
Browser Output
A is equal to B

You can use the elseif statement several times at once, like the below example –

<?php
$day = 6;

if ($day == 1) {
    echo "Today is sunday";
}
elseif ($day == 2) {
    echo "Today is Monday";
} 
elseif($day == 3){
	echo "Today is Tuesday";
}
elseif($day == 4){
	echo "Today is Wednesday";
}
elseif($day == 5){
	echo "Today is Thursday";
}
elseif($day == 6){
	echo "Today is Friday";
}
elseif($day == 7){
	echo "Today is Saturday";
}
else {
    echo "Day not found!";
}
?>
Browser Output
Today is Friday

Leave a Reply

Your email address will not be published. Required fields are marked *