PhpMyanmar.com logo Php Tutorial
Home
Php Tutorial
 Echo
 If

Elseif
Show Myanmar Version
 show-myanmar-font
Printer Friendly Page

Just like an "if statement", an "elseif statement" also contains a conditional statement, but it must be preceded by an "if statement".

You cannot have an "elseif statement" without first having an "if statement".
  • When PHP evaluates your "If...elseif...else statement" it will first see if the "If statement" is true.
  • If that tests comes out false it will then check the first "elseif statement".
  • If that is still false it will either check the next "elseif statement", if one exists.
  • If there are no more "elseif statements", it will evaluate the "else statement", if one exists

Example for Elseif.

PHP Code:
$employee = "Sam";

if($employee == "Ms. Smith"){
      echo "Hello Ma'am";
} else {
      echo "Morning";
}

Now, if we wanted to also check to see if the big boss Sam was the employee we need to insert an elseif clause.

PHP Code:
$employee = "Sam";

if($employee == "Ms. Smith"){
      echo "Hello Ma'am";
} elseif ($employee == "Sam"){
      echo "Good Morning Sir!";
} else {
      echo "Morning";
}
Display:
Good Morning Sir!

PHP first checked to see if $employee was equal to "Ms. Smith", which evaluated to false.

Next, PHP checked the first "elseif statement. $employee did in fact equal "Sam" so the phrase "Good Morning Sir!" was printed out. 

If we wanted to check for more employee names we could insert more "elseif statements.

Remember that an "elseif statement" cannot be used unless it is preceded by an "if statement!

Note
For more detail please see Php Manual at http://www.php.net

Processing time -- 0.0138 seconds.

4d5836947f97286791ecc5752ce38ccb