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

Chris Seals
Chris Seals
4,507 Points

The struggle is real

The following statements bring back nothing. I am struggling to understand why:

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

Here is how I am thinking about the problem, correct me:

  1. $username cannot equal treehouse, but it does, so we do not echo "hello $username"
  2. ($username) evaluates as true, and therefore does not meet the criteria for else, and therefore we also do not echo "you must be logged in"?

Moderator edited: Added markdown so that the code renders properly on the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I took the liberty of adding some markdown to your code so that it's more easily readable. If you have a moment, check out the Markdown Cheatsheet at the bottom of the "Add an Answer" section for instructions on how to properly format code for the forums. :sparkles:

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Your thought process is correct. Because $username evaluates to a truthy value, the else will never occur. So the first if statement block is executed. However, it contains another if statement. That one says if the username is not "Treehouse". But it is Treehouse, so this evaluates to a false, meaning that the block inside it won't execute either.

Hope this helps! :sparkles:

Chris Seals
Chris Seals
4,507 Points

One small additional question: If the variable "$username" was never previously defined, would it still be considered a truthy statement?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I think I may have misinterpreted what you were asking. Let's say $x was never defined.

if($x) {
  echo "This is true.";
}

The above code will result in an error.

This, however, will not:

if($x = 10) {
    echo "This is true.";
} 

Because 10 is a truthy value, this works even on a previously undefined variable. But you cannot use an undefined variable and still not define it in the if statement.

Probably be better to code it like this. First statement checks if the Username exists AND if the username is NOT equal to $username (In this case, Treehouse)

$username = "Treehouse";

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

Code above will print 'You must be logged in'

However, If we change $username to another value such as Tim - It will execute the first statement 'Hello Tim' as $username is NOT EQUAL to 'Treehouse'