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

Michael Bragg
Michael Bragg
811 Points

If statement for $flavor try 2

I have been trying to figure out the proper syntax for this, and this is my second attempt, and still incorrect.

index.php
<?php
$flavor = "vanilla";
if ($flavor == "vanilla") 
{ echo "vanilla";   
 else if
{echo "cookie dogh;
}

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

?>

3 Answers

The opening and closing php tags (<?php & ?>) in the second and fourth echo statements aren't necessary since you are still in 'php mode' from the php tags on the first and last lines.

Also, you don't want to output the last message unless your favorite flavor is the same as Hal's, so you can just wrap that in a conditional statement. It will only be echo'ed if $flavor is equal to 'cookie dough'.

This should help you pass:

<?php
$flavor = 'Dirt';

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

?>
Michael Bragg
Michael Bragg
811 Points

Why is one answer using single quotes, and the other double quotes?

In the snippet that I provided, anything that was in double quotes is what treehouse had already written in the code challenge. The two lines that are in single quotes ('Dirt' and 'cookie dough') is what I wrote myself. I did this out of personal preference, I tend to use single quotes unless I need to do some string interpolation, which can't be done with single quotes.

So, while there are differences between single and double quotes for this code challenge it make no real difference.