Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Creating the Menu and Footer Variables and Conditionals

STAGE 2 CHALLENGE TASK 3 OF 4

The message in the final echo command only makes sense if your favorite flavor is the same as mine. 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
$flavor = "vanilla";
echo "$flavor";
if ($flavor == "cookie dough");
echo "<p>Your favorite flavor of ice cream is ";
echo "vanilla";
echo ".</p>";
echo "<p>Randy's favorite flavor is cookie dough, also!</p>";

?>

2 Answers

Greg Kaleka
Greg Kaleka
39,019 Points

Hi there!

You might want to take another look at the video (often, this is a good thing to do if a code challenge is stumping you) to see how to write if statements in PHP. Here's the solution, but make sure you really understand it before moving on. You may also want to have a look at the PHP documentation for if statements.

<?php

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

?>
Erik McClintock
Erik McClintock
45,783 Points

You're not writing your if statement correctly. You currently have a semi-colon after the condition, which is ending the line, and your if statement thus isn't doing anything. If you recall from your lessons, after your conditional, you need to open up a set of curly braces to put code inside, so that IF the condition evaluates to true, that code will run, otherwise, it will not. You are also not replacing the hard coded "vanilla" string with the value of your variable ($flavor), and you are not writing the conditional in the correct place based on the instructions given: the task tells you to wrap the final echo command in your if statement.

You may benefit more from going back and rewatching some videos to really cement the syntax of these different aspects of the language in your head. It is absolutely paramount that you memorize how these look and work, or you will never truly understand the language or be able to write your own code.

Erik