Showing posts with label if. Show all posts
Showing posts with label if. Show all posts

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

Thursday, 8 April 2021

Php: if statement

"if" tell the program execute the section of code when the condition evaluates to true.

 

Syntax

if(condition){

         // Executes if the condition true

}

 

Example

if ($a % 2 == 0) {

    echo "a is even number";

}

 

Find the below working application.

 

if_condition_demo.php

#!/usr/bin/php

<?php
$a = 10;
$b = 11;

if ($a % 2 == 0) {
    echo "a is even number";
}

if ($b % 2 == 0) {
    echo "b is even number";
}

?>

 

Output

$./if_condition_demo.php 

a is even number

 

Alternative if syntax

if (condition) :

 

endif;

 

Example

if ($a % 2 == 0) :

    echo "a is even number";

endif;

 

if_alternative_syntax_demo.php

#!/usr/bin/php

<?php
$a = 10;

if ($a % 2 == 0) :
    echo "a is even number";
endif;

?>

 

Output

$./if_alternative_syntax_demo.php 

a is even number

 

 

 

 

 

 

Previous                                                    Next                                                    Home