IF ELSE IF Statements

Chapter 6 #114 - Martha's CIS 52

ASSIGNMENT/EXPLANATION

Programmers can use an if, elseif, else clause to test multiple expressions (the if...else part), before adding a default clause (the elseif portion). Scripts can have as many elseif clauses as needed, and a default action is not necessary.

SYNTAX:

if (expression) {
  // code executed if expression evaluates to true 
} elseif (another_expression) 
  // code executed if previous expression failed
  // and this one evaluates to true
} else {
  // code executed in all other cases
} 

Exercise

Awww, Don't be down!

CODE

$mood = "sad";
if ($mood == "happy") {
echo "Hooray, I'm in a good mood!";
} elseif ($mood == "sad") {
echo "Awww, Don't be down!";
} else {
echo "Neither happy nor sad but $mood.";
}