PHP Build a Basic PHP Website (2018) Building a Media Library in PHP Variables and Conditionals

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>";
}

?>

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.