Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Building a Media Library in PHP Variables and Conditionals

Build a Basic PHP Website task 3 of 3

Challenge Task 3 of 3

The message in the final echo command only makes sense if your favorite flavor is the same as Hal's. Add a conditional around that final echo command that checks if the flavor variable has a value of "cookie dough." (Remember to choose carefully between using a single equal sign and a double equal sign in the check.) Preview the code and, if you have a different flavor, make sure the message disappears.

index.php
<?php

echo "<p>Your favorite flavor of ice cream is ";
echo "vanilla";
echo ".</p>";
echo "<p>Hal's favorite flavor is cookie dough, also!</p>";
$flavor ="tomato";
echo"your favorite flavor of ice cream is"; echo $flavor;
echo ".";
if($flavor == ".") echo "So my favorite flavor is, tomato";
echo $flavor; 
echo "tomato"

?>

5 Answers

Challenge Task 3 of 3

The message in the final echo command only makes sense if your favorite flavor is the same as Hal's. Add a conditional around that final echo command that checks if the flavor variable has a value of "cookie dough." (Remember to choose carefully between using a single equal sign and a double equal sign in the check.) Preview the code and, if you have a different flavor, make sure the message disappears.

best answer:

The P versus NP problem

Sean T. Unwin
Sean T. Unwin
28,690 Points

To start with I'm going to take a few steps back to the beginning of the Challenge.

The initial code given is:

<?php

echo "<p>Your favorite flavor of ice cream is ";
echo "vanilla";
echo ".</p>";
echo "<p>Hal's favorite flavor is cookie dough, also!</p>";

?>

Task 1 of the Challenge requests to add the variable $flavor on line 2.

important: Be sure to read the Challenges carefully because as you can see in the pasted code in your post that you put the variable at the end of the code block. This may not seem like a big deal as the code passed that part of the Challenge, however it can possibly lead to issues and even redundancy in later parts of the Challenge.

Task 2 of the Challenge requests to show the variable created in Task 1 instead of static text. To do this we remove the static and replace it with our $flavor variable:

<?php
// replace the following
echo "vanilla";
// with
echo $flavor;

?>

Now we are at the point in the Challenge (Task 3) where your reason for posting is.

Here we are requested to add a conditional statement around the final line given --

<?php
echo "<p>Hal's favorite flavor is cookie dough, also!</p>";
?>

-- with the goal in mind to display that echo statement if the flavors match.

At the beginning of that line, or on an empty line above it if we want to use a full conditional block with curly brackets, we add the conditional (an if statement). We want the conditional statement to check if the two values are the same -- 'cookie dough' == $flavor. Then if they are the same we output (echo) Hal's favorite flavor, which is simply that final echo statement (as shown in the sentence). That is all that is needed to pass the challenge and we don't have to add any extra lines of code, unless we use a full conditional block with the curly brackets.

I hope that helps you to pass the Challenge without having to explicitly show the final code. :)

Challenge Task 1 of 3

This block of code displays two sentences related to flavors of ice cream. In this code challenge, we'll modify the code block to use a variable and a conditional. First, create a new variable named flavor and assign your favorite flavor as the value. (Add this command at line 2. Don't forget to use a dollar sign in the variable name, and don't forget to end your command with a semi-colon.)

best answer :

<?php echo $flavor =" ";

Challenge Task 2 of 3

Modify the command that displays the flavor of ice cream. Instead of it displaying a static piece of text, change it to display the value stored in the 'flavor' variable.

Best answer : line 7

$flavor = " tomato my favourite colour ";

Brock Higham
Brock Higham
6,311 Points

<?php $flavor = "vanilla"; $hal_flavor = "cookie dough"; echo "<p>Your favorite flavor of ice cream is "; echo $flavor; echo ".</p>"; if ($flavor == $hal_flavor) echo "<p>Hal's favorite flavor is cookie dough, also!</p>";

?>