Conditionals and Control Flow 2/4
If statements
Nice work on comparisons! Now let's see how we can use comparisons to ask yes or no questions.
Say we want to write a program that asks whether your name is longer than 7 letters. If the answer is yes, we can respond with "You have a long name!" We can do this with an
if
statement:<?php
$age = 17;
if( $age > 16 ) {
echo "You can drive!";
}
?>
An if statement is made up of the
if
keyword, a condition like we've seen before, and a pair of curly braces { }
. If the answer to the condition is yes, the code inside the curly braces will run.
Instructions
<html>
<head>
</head>
<body>
<p>
<?php
$items = 7;
if($items > 5) {
echo "You get a 10% discount!";
}
?>
</p>
</body>
</html>
Comments
Post a Comment