• Tutorial List

PHP if, if-else and if-elseif-else Statement

Last updated May 15, 2020
PHP if, if-else and if-elseif-else Statement

What are the Conditional Statements?

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


PHP if Statement

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

Syntax of the PHP if statement

<?php
  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

Another Example:

<?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 if-else Statement

The PHP else block executes when the specified condition is false.

Syntax of the if-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 if-elseif-else Statement

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

Basically, the elseif block is used to executes different codes for more than one condition.

Syntax of the 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 following 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