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

Teacher Russell
Teacher Russell
16,873 Points

The username is set, so the first conditional evaluates to true.

Help me out here....What is happening to the variable that it's no longer containing the value "Treehouse" ?:) Thanks!

Quiz Question 2 of 5 What will be displayed in a browser when the following PHP code is executed:

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

1 Answer

andren
andren
28,558 Points

Nothing happens to the variable, it still contains "Treehouse".

As you state $username is set so the first if statement runs. The if statement that is nested inside checks if $username is not equal to "Treehouse", since it is actually equal to that the if statement does not run. The else statement in the code is attached to the first if statement, and since that did run the else statement does not run.

Meaning that ultimately nothing gets displayed after running the code.

Here is a version of the question code with some comments added:

<?php
$username = "Treehouse"; # Define $username and set it to "Treehouse"
if ($username) { # Check if $username is truthy (True)
    if ($username != "Treehouse") { # Check if $username is not "Treehouse" (False)
        echo "Hello $username"; # This does not run because the condition above is `False`.
    }
} else { # Run if connected `if` statement above did not run
    echo "You must be logged in"; # This does not run because the `if` statement above it ran.
}