Conditionals and Control Flow 3/4
Adding an Else
Great! We used an
if statement to do something if the answer to the condition was yes, or true as we say in PHP.
In addition to doing something when the condition is
true, we can do something else if the condition is false. We can do this using an if / else statement:<?php
$name = "Edgar";
if ($name == "Simon") {
print "I know you!";
}
else {
print "Who are you?";
}
?>
Just like before, if the condition is
true, then only the code inside the first pair of curly braces will run. Otherwise, the condition isfalse, so only the code inside the second pair of curly braces after the else keyword will run.
In the example above the condition
$name =="Simon" evaluates to false since $name isEdgar. Since the condition is false, only the code inside the curly braces after the elsekeyword runs, and prints out Who are you?
Instructions
Under your
if statement on line 12, write anelse statement to capture the people who are only buying 5 items or fewer. In their case, use echo to output "You get a 5% discount!"<html>
<head>
</head>
<body>
<p>
<?php
$items = 3;
if($items > 5) {
echo "You get a 10% discount!";
}
else {
echo "You get a 5% discount!";
}
?>
</p>
</body>
</html>
Comments
Post a Comment