PHP 5 – if…else…elseif Statements



PHP – The if Statement

If statement is a control statement. The body of the if statement is executed when condition is true or nonzero. In PHP we can pass either Boolean value or any other value.

Syntax

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

Example

 <?php $a=10; $b=10; if($a==$b) { echo"successful"; } ?>

PHP – The if…else Statement
It is also a control statement. It is very useful in programming. If condition is not true the else part executed. Else has no condition.

Syntax

if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

Example

<?php $a=10; $b=10; if($a==$b) { echo"successful"; } else { echo"failure"; } ?>

PHP – The if…elseif….else Statement
Syntax

if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}

Example

<?php $a=10; $b=10; $c=10; if($a > $b and $a > $c)
{
echo"a is largest.";
}
elseif($b > $a and $b > $c)
{
echo"b is largest";
}
elseif($c > $a and $c > $b)
{
echo" c is largest";
}
else
{
echo"a=b=c";
}
?>