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 Simple PHP Application Creating the Menu and Footer Variables and Conditionals

conditional php

In the last part of this code, I am trying to check and see if flavor is cookie dough and if it is not, then have no message show up. Please advise.

<?php
$flavor = "vanilla";

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

3 Answers

Stone Preston
Stone Preston
42,016 Points

Your conditional looks correct. However it looks like you already have a php tag open at the very start of your code so you dont need to open a new set of php tags around your conditional which could be causing some issues.

<?php
$flavor = "vanilla";

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

try removing the php tags around your condition and see if it works

//opening tag
<?php
$flavor = "vanilla";

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

//closing tag
?>

Okay, that worked, but then when I went to the next step, it is saying that step 3 is not working (when I change favorite variable to cookie dough). Code is on bottom. Please advise Thank you

<?php $flavor = "cookie dough";

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

?>

Hi Edward,

I noticed that in the code you are providing the html tags are missing. Did you pass any of the code challenges that way? Anyhow, here is the correct code for the code challenge:

<?php
$flavor = "cookie dough";

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

?>
Stone Preston
Stone Preston
42,016 Points

he has them, they just dont show up without formatting code first. Ive edited his post so that its formatted