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

Cindi Harris
Cindi Harris
3,702 Points

Could i please get an explanation of this problem?

I have went over this multiple times. I seem to understand it as if there is a username present and if that username is NOT equal to Treehouse, which in this case it is so i would think that it would not echo Hello $username or Hello Treehouse. I would think it would return You must be logged in. I know there is something i am missing in this case. Can you help me identify what error i'm making?

1 Answer

28,558 Points

The thing you are missing is that the else clause that echos "You must be logged in" is attached to the first if statement, it will only execute if the first if statement is false. The second if statement nested within the first if statement is a completely independent statement that is not in any way affected by the if/else statements that exist above it.

It is simply an if statement that checks if the username is not equal to Treehouse. It has no else statement attached to it so when it ends up being false it simply does nothing. And since the if statement it is nested under actually did run the else statement will not run.

Maybe it gets clearer if I actually modify the example a bit to illustrate how independent these statemented are:


$username = "Treehouse";
if ($username) { // No nesting
    if ($username != "Treehouse") { // One level of nesting
        echo "Hello $username";
    } else { // One level of nesting
        if($username == "Treehouse") { // Two levels of nesting
            echo "Hello World";
} else { // No nesting
    echo "You must be logged in";


This is an example with even more nesting in it that the one in the quiz. But based on the explanation I gave above what do you think the program above will output?

The answer is Hello World.

username contains a string so the first if statement runs, username does contain Treehouse so the nested if statement does not run. This causes the else statement attached to the nested if statement to run and within there is another if statement that checks if username equals Treehouse. Since that is true it echoes out "Hello World" and then it is done executing.

I'm not sure if that example made things clearer or more confusing, but the point I'm trying to illustrate is that the if statements are independent of one another. The else statements only care about whether the if statement they are attached to ran or not. They are not aware of and do not care about the result of any other if statement nested or otherwise.