Learn

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.

We’re creating a form with a checkbox. We want the checkbox to be colored green if the answer is correct or red if the answer is incorrect.

To start, define a function called markAnswer(). The function should take a single boolean value as an argument. This argument will be TRUE if the answer is correct or FALSE otherwise.

2.

Now, it’s time to write the body of the function!

If the answer was correct, the markAnswer() function should return "green". Otherwise, the function should return "red".

3.

Time to test your function! You’ll want to invoke the markAnswer() function twice — once with TRUE as an argument and again with FALSE. To ensure the function returns the correct values, use print or echo statements to print the result.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?