Learn
Booleans and Comparison Operators
If Statements

We’re going to learn about a specific type of conditional called an if statement. An if statement follows this basic structure:

if (/*some condition*/) { // Do something... }

The parentheses hold the condition we want the computer to check. If the condition is true, the code inside the code block ({ }) will run; if it’s not true, the code will not run.

The foundation of any conditional is the boolean data type. A boolean can have one of two values: TRUE or FALSE. Note that these are the words without quotation marks—the string "TRUE" is not the same as the boolean value TRUE. These values are not case sensitive, but we follow the convention of making them uppercase.

If we wanted to write code to approximate our hyperlink example from the previous exercise, we might write something like this:

$is_clicked = TRUE; if ($is_clicked) { $link_color = "purple"; echo $link_color; }

In the code above, we ask the computer to check the $is_clicked variable as the condition. If its value is TRUE, the value of $link_color will be assigned "purple" and printed.

In the code above, were our condition not met, we’d skip over the code turning the link purple, but what should we do instead? If the link has been clicked, the color should be purple, otherwise it should be blue. We can include a block of code to run when the condition is not met with the keyword else:

$is_clicked = FALSE; if ($is_clicked) { $link_color = "purple"; echo $link_color; } else { $link_color = "blue"; echo $link_color; }

We changed the value of $is_clicked to FALSE so that the if block will not run. Rather, the else block will run and blue will be printed to the terminal.

Let’s make some conditionals!

Instructions

1.

You’re going to write a function that changes a check box depending on whether the answer given was correct or incorrect.

Write a function markAnswer(). Your function should expect two arguments:

  1. The first is a boolean value—if the answer was correct, this value will be TRUE otherwise it should be false.
  2. The second argument will be an associative array representing the checkbox. Since this function will be making permanent changes to the box, it should be passed by reference to the function.
2.

Now to write the meat of the function!

If the answer was correct, the function should change the value at the box’s key of "shape" to "checkmark" and the value at the box’s key of "color" to "green". Otherwise, the function should change the value at the box’s key of "shape" to "x" and the value at the box’s key of "color" to "red".

3.

Time to test your function! We’ve provided two associative arrays for you—each represents a different learner. Invoke your function twice:

  • First invoke markAnswer() passing in the value at "is_correct" for $learner_one as well as their "box".
  • Next, invoke markAnswer() passing in the value at "is_correct" for $learner_two as well as their "box".
  • Finally, use print_r() to print each learners box and make sure they’ve changed as expected.
Folder Icon

Take this course for free

Already have an account?