PHP if…elseif….else Statement

The php if...elseif....else Statement is used to generate more than two conditions in one script. The php script will verify each statement line by line and execute the code as per the condition value is true or false. The php if statement runs till it finds the true statement. If it doesn’t found until the end of the script then it uses the default echo statement.

Example:

[php]<?php
$t = "25";

echo "<p>The total pages are " . $t;

echo ", what is your status :</p>";

if ($t < "10") {
echo "I haven’t complete it";
} elseif ($t < "20"){
echo "I have completed";
} else {
echo "I will let u know";
}
?>[/php]

In the above example, we are running two conditions. The first if statement checks for the condition. It says the $t is smaller than 10. Which in this case is false because as per our variable $t, we have assigned value 25. That means the script will move one from the if statement and check for the elseif statement.

In the elseif statement we have are saying the $t is small then 20 which is again a false statement. so again the php will move ahead to the next statement. Our last and default echo statement is “I will let u know“. The script will show the outcome of the final echo statement because of none of the above if statement matches the condition.

The result of the above statement will be as given below.

The total pages are 25, what is your status :

I will let u know

Related Posts

Leave a Reply