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

dominick lucero
dominick lucero
788 Points

Can someone please explain to me why this answer is "nothing is displayed" as opposed to "you must be logged in"?

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

So the first conditional is met ($username is set) but the second is not ($username IS equal to treehouse). How is it then that the answer is "Nothing will be displayed"? I thought it would echo "You must be logged in". Where am I going wrong?

Thanks peeps!

1 Answer

andren
andren
28,558 Points

The else statement is attached to the first if statement. The nested if statement is completely independent of the if/else statement that exists above it. Since the first if statement fires the else statement attached to it will not fire, and since the nested if statement has no else statement attached to it nothing happens when it doesn't fire.

dominick lucero
dominick lucero
788 Points

Ohhh ok I get it now, thank you!