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 PHP Arrays and Control Structures PHP Conditionals Conditionals

I don't understand the answer to this one...

$username = 'Treehouse'; The second if statement states if it doesn't equal Treehouse, than echo 'Hello $username'; but $username = 'Treehouse'.
I first responded to this with You are logged in, but that was wrong. Then I tried Hello Treehouse because I thought maybe I was misunderstanding the first conditional statement. Now I am beyond confused with the whole thing... Please help!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! So, I'm going to post the example code here so we can see it a bit more clearly.

$username = "Treehouse";
if ($username) {  // first if statement
    if ($username != "Treehouse") {
        echo "Hello $username";
    }
} else {  // matches the first if statement
    echo "You must be logged in";
}

Ok, so first we set the $username to be Treehouse and you seem to be understanding that part just fine. So now we go down to our first if statement. If the value stored in $username is a truthy value, then we will execute what's inside that block. And this is what happens. We execute what's inside the block.

The trick is here that what's inside that block is yet another if statement. It says that if the name is not "Treehouse", to echo out "Hello $username". But it is "Treehouse" so that echo doesn't happen.

And then we're done. Remember that an else statement matches with an if statement. I put in comments to show the if and the else that match up. If the condition in the if statement evaluates to true (which it did) then the else statement will not be run meaning that the "You must be logged in." echo never happens either.

The end result is that nothing is displayed.

Hope this helps! :sparkles:

Thank you! That makes more sense to me. :)