PHP if..else..else if Statements

PHP if..else..else if Statements are Conditional statement which are used to perform different actions based on different conditions.

We have the following conditional statements in php:

  1. if statement - executes some code if one condition is true
  2. if...else statement - executes some code if a condition is true and another code if that condition is false
  3. if...elseif...else statement - executes different codes for more than two conditions
  4. nested if - The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.

PHP If Statement

The if statement executes block code if one condition is true.
Syntax
if (condition) {
  code to be executed if condition is true;
}

Example

    <?php  
           $month=12;  
           if($month==4){  
                echo "Month $month is April.";  
           }  
    ?>  

Output: Month 4 is April.

 

PHP - The if...else Statement

The if...else statement executes inside code if a condition is true and another code if that condition is false.

Syntax :

    if(condition){  
    //code has been executed if true  
    }else{  
    //code has been executed if false  
   } 

Example :

    <?php  
         $marks=50;  
         if($marks<=40){  
              echo "Your result is fail.";  
         }else{  
           echo "Your result is Pass.";  
        }  
    ?> 

Output: Your result is Pass.

PHP - The if...elseif...else Statement
The if...elseif...else statement is used to check multiple condition.

Syntax:
if (condition) {
  code to be executed if this condition is true;
} else if (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

Example:
$percentage=65;
if($percentage<40)
{
    echo "<br> Fail.";
}else if($percentage<60 && $percentage>=40)
{
    echo "<br> Second class.";
}else if($percentage<70 && $percentage>=60)
{
    echo "<br> First class.";
}else if($percentage<=100 && $percentage>=70)
{
    echo "<br> Distinction class.";
}else
{
      echo "<br> Invalid persantage.";
        
}

?>
Output: First class.

PHP nested if Statement :

The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.

Syntax

    if (condition) {    
    //code to be executed if condition is true   
          if (condition) {    
          //code to be executed if condition is true    
           }    
    }  

Example
    <?php  
        $subject = "Maths";  
        $class= 3;  
      
        if ($class == 3)  
        {  
            if ($subject == "Maths") {  
                echo "Student atttends class-3 maths.";  
            }  
            else {    
                echo "No Class for students";  
            }  
        }  
    ?> 

Output:
Student atttends class-3 maths.

Comments

Leave a Reply

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

28639