Tuesday, 13 April 2021

Php: if – else if - else statement

if-else if-else construct, you can choose an option from number of alternatives. An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

 

Syntax:

if(condition){

}
else if (condition){

}
……
……
else{

}

 

Points to note:

a.   The first ‘if’ condition that evaluates to true will run and rest all will be ignored.

b.   If no condition evaluates to true, then else block code gets executed.

 

Example

 

if ($percentage < 35)
    echo "You are failed";
else if ($percentage < 50)
    echo "You are passed and got third class";
else if ($percentage < 60)
    echo "You are passed and got second class";
else if ($percentage < 70)
    echo "You are passed and got first class";
else
    echo "You are passed and got distinction";

 

if_else_if_ladder.php

#!/usr/bin/php

<?php

function print_student_percentage($percentage)
{
    if ($percentage < 35)
        echo "Percentage : $percentage. You are not passed\n";
    else if ($percentage < 50)
        echo "Percentage : $percentage. You are passed and got third class\n";
    else if ($percentage < 60)
        echo "Percentage : $percentage. You are passed and got second class\n";
    else if ($percentage < 70)
        echo "Percentage : $percentage. You are passed and got first class\n";
    else
        echo "Percentage : $percentage. You are passed and got distinction\n";
}

print_student_percentage(55);
print_student_percentage(17);
print_student_percentage(98);
?>

 

Output

#!/usr/bin/php

<?php

function print_student_percentage($percentage)
{
    if ($percentage < 35)
        echo "Percentage : $percentage. You are not passed\n";
    else if ($percentage < 50)
        echo "Percentage : $percentage. You are passed and got third class\n";
    else if ($percentage < 60)
        echo "Percentage : $percentage. You are passed and got second class\n";
    else if ($percentage < 70)
        echo "Percentage : $percentage. You are passed and got first class\n";
    else
        echo "Percentage : $percentage. You are passed and got distinction\n";
}

print_student_percentage(55);
print_student_percentage(17);
print_student_percentage(98);
?>

 

Alternative syntax

if(condition):
    .....
    .....
elseif(condition):
    .....
    .....
elseif(condition):
    .....
    .....
endif;

 

Note: There is no space between else and if, it is written as elseif.

 

Example

if ($percentage < 35) :
    echo "Percentage : $percentage. You are not passed\n";
 elseif ($percentage < 50) :
    echo "Percentage : $percentage. You are passed and got third class\n";
 elseif ($percentage < 60) :
    echo "Percentage : $percentage. You are passed and got second class\n";
 elseif ($percentage < 70) :
    echo "Percentage : $percentage. You are passed and got first class\n";
else :
    echo "Percentage : $percentage. You are passed and got distinction\n";
endif;

 

if_else_if_ladder_alternative.php

#!/usr/bin/php

<?php

function print_student_percentage($percentage)
{
    if ($percentage < 35) :
        echo "Percentage : $percentage. You are not passed\n";
     elseif ($percentage < 50) :
        echo "Percentage : $percentage. You are passed and got third class\n";
     elseif ($percentage < 60) :
        echo "Percentage : $percentage. You are passed and got second class\n";
     elseif ($percentage < 70) :
        echo "Percentage : $percentage. You are passed and got first class\n";
    else :
        echo "Percentage : $percentage. You are passed and got distinction\n";
    endif;
}

print_student_percentage(55);
print_student_percentage(17);
print_student_percentage(98);
?>

 

Output

$./if_else_if_ladder_alternative.php 

Percentage : 55. You are passed and got second class
Percentage : 17. You are not passed
Percentage : 98. You are passed and got distinction

 

 

 

 

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment