Posted By:
9 Mar, 2014 12:36 am PST

How to write if----elseif statement block in php?

How to write if----elseif statement block in php?

This is extension of if--else flow statements. When you want to take decision against multiple condition, in this situation if—elseif control structure is used. When condition is evaluated to be true, associate block of code gets executed.

Syntax:-

If(condition)

{

            //write your code here, if condition is true

}

elseif(condition)

{

          //write your code here, if elseif condition is true

}

else

{

           //write your code here, if all condition is false

}

 
 

<?php

echo " <h2 > " ;

echo " Assign Class ( distinction ( >=75) , first class ( >=60) , pass ←

class ( >=40) , fail ( <40) ) based on marks obtained " ;

echo " <br > < br > " ;

$marks = rand (20 ,100) ;

// rand ( int min , int max ) this function returns a pseudo - random integer between // " min “ and " max " value .

echo " Marks obtained by X student is { $marks } <br > < br > " ;

$cls = " none " ;

if ( $marks >= 75) // condition

{

    $cls = " Distinction " ;

}

else if ( $marks >= 60) // condition

{

     $cls = " First Class " ;

}

else if ( $marks >= 40) // condition

{

     $cls = " Pass Class " ;

}

else

{

      $cls = " Fail " ;

}

echo " Student X got \"{ $cls }\" class " ; 

?>

ElseIfStatement.php

 

 


 

Else--If Output

 

 



How to you if...elseif....else Statement?

 

It is used to select one of several blocks of code that need to be executed.

//To find greatest of three numbers

 

<!DOCTYPE html>

<html>

<body>

<?php

   $a=10;

   $b=20;

   $c=30;

   if(($a>$b)&&($a>$c))

   {

       echo " a is greater ";

   }

   else if(($c>$b)&&($c>$a))

   {

       echo " c is greater";

   }

   else

   {

       echo" b is greater";

   }

?>

</body>

</html>

 

/*Output of the above program is:

c is greater

*/

 

Attachment: ifelseif.php
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud