PHP ElseIf Construct

In this tutorial we will study about elseif in PHP. How to write elseif body part, what are the constraints to use etc. Aare discussed in this tutorial. Examples in this tutorial will make it more clear.

 
 

Share on Google+Share on Google+

PHP ElseIf Construct

Ads

In this tutorial we will study about elseif in PHP. How to write elseif body part, what are the constraints to use etc. Aare discussed in this tutorial. Examples in this tutorial will make it more clear.

Elseif construct:

Combination of else and if is known as elseif or else if. It is executed only when the if portion is false, but unlike else, we can put any condition with it. The conditions will be check only when the if part is false.

In a program there could be several 'elseif' block, we can write nested 'elseif' also, the elseif statement will execute only if the preceding if and elseif statements are false.

Example:

<?php

$a=13;

$b=44;

$c=142;

if($a>$b)

{

if($a>$c)echo '$a is greatest';

}

elseif($b>$c) echo '$b is greatest';

else echo '$c is greatest';

?>

 

Output:

$c is greatest

Example:

<?php

$a=1333;

$b=144;

$c=142;

if($a>$b && $a>$c) echo '$a is greatest';

elseif($b>$c && $b>$a) echo '$b is greatest';

else echo '$c is greatest';

?>

 

Output:

$a is greatest

Advertisements

Ads

Posted on: March 25, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles