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

Jack Wuerfel
Jack Wuerfel
373 Points

I have no clue how to do this question!

I'm stuck on this question. I don't know how to write the else part of the conditional. It asks to hide the message and change the variable value to cookie dough....

index.php
<?php
$flavor="chocolate";
echo "<p>Your favorite flavor of ice cream is ";
echo "$flavor";
echo ".</p>";
if($flavor =="chocolate"){
echo "<p>Randy's favorite flavor is $flavor, also!</p>";
}
?>
Jack Wuerfel
Jack Wuerfel
373 Points

Figured it out. Thanks for the help

3 Answers

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

Task 3 of this challenge says:

The message in the final echo command only makes sense if your favorite flavor is the same as mine. Add a conditional around that final echo command that checks if the flavor variable has a value of "cookie dough."

This is equivalent to this example.

Topic: hockey.

I love this topic. You love basketball too.

In any language, this would be confusing because basketball and hockey are 2 different topics. So if you think about the code, that second sentence would only make sense if TOPIC was equal to HOCKEY. You don't need an if/else here, you only need an if.

<?php

$topic = 'hockey';
echo "I love $topic";

if($topic == 'hockey'){
   echo "You love $topic too.";
}
?>

As you can see rom the above example, I'm setting a variable to a value of hockey. Then echoing out that I love hockey. Then I'm checking that if the variable is equivalent hockey, then echo out that you love it too.

<?php

$topic = 'basketball';
echo "I love $topic";

if($topic == 'hockey'){
   echo "You love $topic too.";
}
?>

In this example, the topic DOES NOT equal hockey, so the second echo would not be reached.

You can always go back and watch the video if you're totally stuck ;)

An else block goes write after the if statement - see below

<?php

if ($flavor =="chocolate"){
    echo "<p>Randy's favorite flavor is $flavor, also!</p>";
} else {
    // the flavor isn't chocolate, do something else instead!
}
Henrique Zafniksi
Henrique Zafniksi
8,560 Points

The 'else' goes after the 'if' expression, and is triggered whenever the if condition returns false. That means that if the $flavor is not chocolate, the value will be changed to cookie dough.

<?php 
    $flavor = "chocolate";
    if ($flavor == "chocolate") {
        echo "<p>Randy's favorite flavor is $flavor</p>";
    } else {
        $flavor = "cookie dough";
    }
?>