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

PHP Variables and Conditionals. What went wrong?

this code challenge 2/4 task for Variables and Conditionals is driving me insane. took me about 2 hours figuring out with no solution after reviewing previous videos various times.

Here is the question: "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."

My Code:

<?php
$flavor = "<p>Your favorite flavor of ice cream is ";
echo $flavor;    <!--NEWLY ADDED-->
echo "vanilla";
echo ".</p>";
echo "<p>Randy's favorite flavor is cookie dough, also!</p>";
?>

What went wrong?

It works : <?php $flavor = "mint"; echo "<p>Your favorite flavor of ice cream is "; echo $flavor ; echo ".</p>"; echo "<p>Randy's favorite flavor is cookie dough, also!</p>"; ?>

12 Answers

<?php $flavor = "chocolate"; echo "<p>Your favorite flavor of ice cream is "; echo "chocolate"; echo ".</p>"; echo "<p>Randy's favorite flavor is chocolate, also!</p>";

?>

what I finally figured out and was getting tripped up on is---$flavor needs an actual flavor ie choco, vanilla, banana then the 1 echo statement must match that favor and finally Randy's favorite flavor must be the same in order to work

This Challenge is broken Fix Team i had my code 1000000000000% correct and would not pass

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I actually had cookie dough ice cream twice today!

You ate something in your code :)

This code should work for you

<?php

$flavor="choco"; // this is my fav flavor

echo "<p>Your favorite flavor of ice cream is "; // missing line in you code

echo $flavor;

echo ".</p>";

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

?>

I tried running your code through the code challenge multiple times, removing your comments and additional space. It does not pass the challenge.

OMG!! thank YOU guys! saved me from suicide!

hi guys, this discussion was about task 2/4, I got a question about task 3/4 of the same challenge. unfortunately, I don't get it to work. my attempt looks like this:

<?php $flavor = "vanilla"; if ($flavor == "cookie dough") { echo "<p>Your favorite flavor of ice cream is "; echo "$flavor"; echo ".</p>"; }

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

?>

first sentence disappears, but it won't let me pass the challenge??

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It's the second sentence that needs to be wrapped in the conditional. The page should always say this:

Your favorite flavor of ice cream is strawberry.

However, it should only display the second sentence if your flavor is the same as mine.

Your favorite flavor of ice cream is cookie dough. Randy's favorite flavor is cookie dough, also!

Does that help?

yes, thanks a lot! ..it worked

Hey, for me not any of these replies helped me, compare my code to your own. The moderators comment wasn't too helpful either.

<?php
$flavor = "strawberry";
echo "<p>Your favorite flavor of ice cream is ";
echo "$flavor";
echo ".</p>";
echo "<p>Randy's favorite flavor is cookie dough, also!</p>";

?>

Hey, for me not any of these replies helped me, compare my code to your own. The moderators comment wasn't too helpful either.

<?php
$flavor = "strawberry";
echo "<p>Your favorite flavor of ice cream is ";
echo "$flavor";
echo ".</p>";
echo "<p>Randy's favorite flavor is cookie dough, also!</p>";

?>

Hey, for me not any of these replies helped me, compare my code to your own. The moderators comment wasn't too helpful either.

<?php
$flavor = "strawberry";
echo "<p>Your favorite flavor of ice cream is ";
echo "$flavor";
echo ".</p>";
echo "<p>Randy's favorite flavor is cookie dough, also!</p>";

?>
cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

What should be in the variable as it's value is only whats going to change in the phrase (the dynamic text). In this case, it is the ice cream flavor itself, not the phrase. So It should be like:

<?php

$flavor = vanilla;

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

?>

So, when you change the value of the $flavor variable, it should also change the flavor written in the phrase.