shadowline
Strings - Pulling apart << if/elseif/else >> switch/case
if may be a small word, but it carries a big job. It is a very useful tool in creating a php script.

<?php
if (condition) {
   do this block of coding;
   if the condition carries a true value;
   }
?>

This would be a good time to re-read the operators page. They are commonly used in the condition statement to find the true or false values.

The block of coding within an IF statement will only be looked at IF the condition has a true value. The block of coding will be ignored if the condition has a false value.

<?php
$test = 42;
if ($test > 40) {
   echo "Yes, $test is greater than 40.";
   }
?>



else is used in the situations when you want perform a block of coding if the IF CONDITION has a false value. This is an optional thing you can use.

<?php
if (condition) {
   do this block of coding;
   if the condition carries a true value;
   } else {
   do this block of coding;
   if the condition carries a false value;
   }
?>

Sometimes you only need the IF statement to execute a block of coding upon a certain condition. Other times, you will want to execute either this or that block of coding depending if the condition value is true or false. That is when ELSE comes into play.

<?php
$test = 32;
if ($test > 40) {
   echo "Yes, $test is greater than 40.";
   } else {
   echo "No, $test is less than 40.";
   }
?>



elseif is an extra conditional test within an IF statement. It's like doing a second IF test for a true or false value. The block of coding in an ELSEIF area will be executed only if the condition value of the IFELSE is true. This part goes after the IF and before the ELSE areas.

<?php
if (condition) {
   do this block of coding;
   if the condition carries a true value;
   } elseif (different condition) {
   do this block of coding;
   if the elseif condition carries a true value;
   } else {
   do this block of coding as a default;
   if all above conditions carried a false value;
   }
?>

There could be many ELSEIF statements listed depending on the situation. For our example, we put in only two.

<?php
$test = 32;
if ($test > 40) {
   echo "Yes, $test is greater than 40.";
   } elseif ($test > 35) {
   echo "Yes, $test is greater than 35.";
   } elseif ($test > 30) {
   echo "Yes, $test is greater than 30.";
   } else {
   echo "No, $test must be less than 40 and 35 and 30.";
   }
?>

For those of you who want to see the result :
Yes, 32 is greater than 30.

Changing the value of $test to 22 will result as :
No, 22 must be less than 40 and 35 and 30.
Strings - Pulling apart << if/elseif/else >> switch/case
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow