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

Charles Gooderham
Charles Gooderham
4,053 Points

Why is the answer 'nothing is displayed'?

Can anyone please explain the logic to me in the bottom statement? I know the answer is 'nothing will be displayed' and I believe this is something to do with the if($username) part but can't work out why.

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

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

None of the code blocks are evaluating anything so no strings will actually be echoed.

The String in the variable is equal to the assigned value so that's true and since there's no else block there, it moves on to the else block. Since username is equal to the given value the code treats this as logged in so no message is displayed.

Hope this helps :)