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 trialJack Wuerfel
373 PointsI have no clue how to do this question!
I'm stuck on this question. I don't know how to write the else part of the conditional. It asks to hide the message and change the variable value to cookie dough....
<?php
$flavor="chocolate";
echo "<p>Your favorite flavor of ice cream is ";
echo "$flavor";
echo ".</p>";
if($flavor =="chocolate"){
echo "<p>Randy's favorite flavor is $flavor, also!</p>";
}
?>
3 Answers
Mike Costa
Courses Plus Student 26,362 PointsTask 3 of this challenge says:
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."
This is equivalent to this example.
Topic: hockey.
I love this topic. You love basketball too.
In any language, this would be confusing because basketball and hockey are 2 different topics. So if you think about the code, that second sentence would only make sense if TOPIC was equal to HOCKEY. You don't need an if/else here, you only need an if.
<?php
$topic = 'hockey';
echo "I love $topic";
if($topic == 'hockey'){
echo "You love $topic too.";
}
?>
As you can see rom the above example, I'm setting a variable to a value of hockey. Then echoing out that I love hockey. Then I'm checking that if the variable is equivalent hockey, then echo out that you love it too.
<?php
$topic = 'basketball';
echo "I love $topic";
if($topic == 'hockey'){
echo "You love $topic too.";
}
?>
In this example, the topic DOES NOT equal hockey, so the second echo would not be reached.
thomascawthorn
22,986 PointsYou can always go back and watch the video if you're totally stuck ;)
An else block goes write after the if statement - see below
<?php
if ($flavor =="chocolate"){
echo "<p>Randy's favorite flavor is $flavor, also!</p>";
} else {
// the flavor isn't chocolate, do something else instead!
}
Henrique Zafniksi
8,560 PointsThe 'else' goes after the 'if' expression, and is triggered whenever the if condition returns false. That means that if the $flavor is not chocolate, the value will be changed to cookie dough.
<?php
$flavor = "chocolate";
if ($flavor == "chocolate") {
echo "<p>Randy's favorite flavor is $flavor</p>";
} else {
$flavor = "cookie dough";
}
?>
Jack Wuerfel
373 PointsJack Wuerfel
373 PointsFigured it out. Thanks for the help