CONTROL FLOW: IF/ELSE

Comparisons(1/4)

So far we've seen:

strings (e.g. "dogs go woof!") numbers (e.g.4, 10) Now let's learn about comparison operators.

List of comparison operators:

>Greater than

< Less than

<=Less than or equal to

>=Greater than or equal to

==Equal to

!=Not equal to

Instructions

On line 8, use a comparison operator to compare two numbers. Make sure to end your line of code with a semicolon.

Hint

Here's how to compare that 6 is less than 7:

<p>

  <?php
    6 < 7;
  ?>

</p>

index.php

<html>
  <head>
    <title>Comparing Numbers</title>
  </head>
  <body>
    <p>
      <?php
         6 < 7;
      ?>
    </p>
  </body>
</html>

If statements(2/4)

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 ifstatement:

<?php
  $age = 17;

  if( $age > 16 ) {
    echo "You can drive!";
  }
?>

An if statement is made up of theifkeyword, 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

On line 7, set$itemsequal to a number greater than 5. Make sure to put a semicolon at the end of the line. On line 9, edit the condition so that your program will print out You get a 10% discount!

Hint

If the condition on line 9 is true, it will run the code in between the curly braces.

index.php

<html>
  <head>
  </head>
  <body>
    <p>
      <?php
        $items =5; // Set this to a number greater than 5!

        if($items < 5) {
          echo "You get a 10% discount!";
        }
      ?>
    </p>
  </body>
</html>

Adding an Else(3/4)

Great! We used an if statement to do something if the answer to the condition was yes, ortrueas we say in PHP.

In addition to doing something when the condition istrue, 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 is false, so only the code inside the second pair of curly braces after the elsekeyword will run.

In the example above the condition $name == "Simon"evaluates to false since$name is Edgar. Since the condition is false, only the code inside the curly braces after theelse keyword 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!"

Hint

Your code should look something like this:

$items = 10;

if($items > 5) {
  echo "You get a 10% discount!";
}
else {
  echo "You get a 5% discount!";
}

index.php

<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>

All Together Now!(4/4)

Great work! Now let's practice using if / else statements. Do as much as you can by yourself, but if you need a reminder, click the "Stuck? Get a hint!" button below.

Instructions

On line 8, write anif /else statement, just like we did in the last exercise. Here's what the outline of the code looked like:

<?php
  if (this condition is true) {
    // do this code
  }
  else {
    // do this code instead
  }
?>
  1. If your condition is true, your code shouldecho "The condition is true"

  2. Otherwise (else) when it is false, your code should echo "The condition is false".

  3. Make sure your condition evaluates tofalse, so that your program prints out"The condition is false".

Hint

Your code could look something like this:

<?php
  if(10 < 3) {
    echo "The condition is true";
  }
  else {
    echo "The condition is false";
  }
?>

index.php

<html>
  <head>
  </head>
  <body>
    <p>
    <?php
        if(10 < 3) {
            echo "The condition is true";
        }
        else {
            echo "The condition is false";
        }
        // Write your if/elseif/else statement here!
     ?>
    </p>
  </body>
</html>