PHP PHP Arrays and Control Structures PHP Conditionals Conditionals

tristan marroquin
tristan marroquin
2,491 Points

Can you explain to me more on why nothing will be displayed is it because if ($username) at the start is false?

Can you explain to me more on why nothing will be displayed is it because if ($username) at the start is false?

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there, tristan marroquin ! I feel fairly certain you are referring to the code in this question:

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

In this instance, nothing will be displayed at all. There is an outer if else statement. The $username starts off being defined as the string "Treehouse". The first if says what to do if the $username is a "truthy" value. A non-empty string is considered "truthy" so the if executes its code. The else will never be fired because the if evaluated to true.

Now, this part is a bit trickier. Inside the first if statement is yet another if statement. It says what to do if the username was not equal to "Treehouse". But it says nothing of what to do if it was equal to Treehouse :smiley: And since, it was equal to "Treehouse" nothing else will happen. So nothing will be displayed.

Hope this helps! :sparkles: