Logo


PHP If Else If Else Statement



If Else If Else Statement

Syntax:

if(condition1)
{
     // execute this block if condition1 is true 
}
else if (condition2)
{
     // execute this block if condition2 is true  
}



else{
     // execute this block if all above conditions are false 
}

Conditional statements are used to perform different actions based on different conditions.

1. if else if else statement are also known as conditional statements. This simply means that they are able to take action or decision based on various conditions.
2. if else if else statement is used to check one blocks of code from many blocks of code.
If the condition is true then the code of block inside if will be executed.
Otherwise the code of block inside else if will be executed.
If all conditions are false then the code of block inside else will be executed.
3. if else if else statement allows the computer to make decisions based on the values of variables which can take the specified action based on the input that is given.
If the decision or condition is true then the code of block inside if will be executed.
If the decision or condition is false then the code of block inside else if will be executed.
If all conditions are false then the code of block inside else will be executed.

You can use this elseif statement many times as you want.
You can just use it one time or 40 times, it does not matter.


Example: 1


<?php
  $name
="mila";
if (
$name=="mila")
  {
    echo 
"hello mila";
   }
else if (
$name=="joe")
   {
    echo 
"hello joe";
    }
else
    {
    echo 
"nobody is here";
    }
?>

Output

hello mila

Example: 2


<?php
  $name
="joe";
if (
$name=="mila")
  {
    echo 
"hello mila";
   }
else if (
$name=="joe")
   {
    echo 
"hello joe";
    }
else
    {
    echo 
"nobody is here";
    }
?>

Output

hello joe

Example: 3


<?php
  $name
="Aspell";
if (
$name=="mila")
  {
    echo 
"hello mila";
   }
else if (
$name=="joe")
   {
    echo 
"hello joe";
    }
else
    {
    echo 
"nobody is here";
    }
?>

Output

nobody is here


If you like this Article, Share and Enjoy



See Also
If Statement
If Else Statement