Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.