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

john paul
john paul
3,191 Points

quiz question

Hi, in this quiz question, the following code did not display anything in the workspace.

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

I answered the question correctly (as ''would not display anything'') but only because I tested my own answer first by writing the code into my work space. I have to admit that I do not fully understand why though, as I first of all expected the answer ''you must be logged in". I am confused as to what the first if statement is attempting to do as their are no conditionals , just if($username). I suspect this must be the reason why nothing is displayed, but am not sure why. Can anyone explain why this code displays nothing. Thank you.

john paul
john paul
3,191 Points

ahhhh!! makes perfect sense, thank you.

Steven Snary
Steven Snary
17,540 Points

I'm glad the answer provided was beneficial - if you feel my answer was adequate please mark it as the accepted or best answer.

Thanks

1 Answer

Steven Snary
Steven Snary
17,540 Points

The outer IF..ELSE equates to true so the code executes the TRUE branch of the outer IF statement (This means that "You must be logged in will never run)

The inner IF..ELSE equates to FALSE so the "Hello $username" will not run

Since there is no ELSE on the inner IF then nothing is displayed and the code exits