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

John O mahoney
John O mahoney
578 Points

What is wrong with this code ???

???

index.php
<?php

$flavor = "Chicken";

echo "<p>Your favorite flavor of ice cream is ";
echo "<?php echo $flavor ?>";

if ($flavor == "cookie dough."){
echo "<p>Hal's favorite flavor is cookie dough, also!</p>";
}
else {
echo ".</p>";
}
?>

1 Answer

Joel Bardsley
Joel Bardsley
31,246 Points

Hi John, I've copied your code below and added comments for things that don't look quite right:

<?php

$flavor = "Chicken";

echo "<p>Your favorite flavor of ice cream is ";
echo "<?php echo $flavor ?>"; // No need to use another set of php tags and an echo statement here

if ($flavor == "cookie dough."){ // this requires the user to enter a full stop after cookie dough for this condition to match
echo "<p>Hal's favorite flavor is cookie dough, also!</p>"; // as $flavor equals cookie dough, you can echo the variable instead
}
else {
echo ".</p>"; // no need for an else statement here, if $flavor doesn't equal cookie dough, it can be ignored. But note that if $flavor does equal cookie dough, your code will have two opening <p> tags and one closing </p> tag.
}
?>

If any of the above is unclear, let me know. Good luck.