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

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Can someone explain the code step by step?

I don't understand why the echo function attached to the second "if" nested statement executes since the "if" statement it's not true ($username != "Treehouse" evaluates to false, or I'm being wrong here?).

3 Answers

andren
andren
28,558 Points

You are correct that the nested if statement will not run. The correct answer to the question is that nothing will be displayed.

Here is a breakdown of the code

<?php
$username = "Treehouse"; # Define username
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.
}

The else statement is attached to the first if statement, not to the nested if statement so it does not run since that first if statement ran.

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

So... for echo to display "Hello $username", both "if" statements (let's call them the PARENT and the nested if statement) need to be true?

From what I understand we could have only these outcomes:

  1. Both if statements (PARENT + nested) evaluate to true - echo prints "Hello $username";
  2. The nested "if" statement evaluates to false so it invalidates the parent "if" statement also... the program will NOT jump to the ELSE statement because the PARENT "if" statement ran already?

If this is the case, in which situation would the ELSE statement run - why did we write it in the first place if from what I understand the program will not get to it anyway?

andren
andren
28,558 Points

So... for echo to display "Hello $username", both "if" statements (let's call them the PARENT and the nested if statement) need to be true?

Yes. If the first if statement is false then nothing within its body (which includes the nested if statement) will run. And the same is true for the nested if statement, the contents of its body (the echo statement) will not run unless it is true.

From what I understand we could have only these outcomes:

There are three possible outcomes from the code:

  1. Both if statements are true. This leads to "Hello $username" being printed.
  2. Only the first if statement is true. This leads to the situation described in my original answer.
  3. The first if statement is false. This leads to the else statement running. Since the else statement only runs when the if statement it is attached to does not run.

The reason why it was written was that it would be triggered if the first if statement was false, which would be the case if $username contained the Boolean false or a Falsy value. Like an empty string or something along those lines.

That is not the case in the question code, but the purpose of the question is to see if you can figure out how PHP will navigate the if/else statements, since that is a good test of your knowledge of how if/else statements work.

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Thank you, excellent answer, made things pretty clear to me (I think the video is a bit too vague for us to understand exactly how nested conditionals work).

I didn't take into consideration the third outcome - where the first "if" statement could be false because in my head it would always evaluate to true. Which takes me to another question: can you give me an example where the first if statement would evaluate to false?

$username = "Treehouse";

if($username == "Alana") == > would this code be considered a good example of a first if statement that would determine the program to jump to the ELSE statement?

andren
andren
28,558 Points

Yes a condition of $username == "Alana" would lead the if statement to not run, and cause the else to run instead.

The thing that ultimately decides if an if statement runs or not is whether it is passed the boolean value true or false or something that ends up evaluating to true or false.

Comparisons like $username == "Alana" are examples of code that evaluates to a Boolean based on wheter or not the comparison ends up being correct or not. Since $username is not equal to "Alana" in your code snippet it would evaluate to false which causes the if statement to not run.

Here is a code example:

<?php
$username = "Treehouse";
if($username == "Alana") {
    echo "Will not print";
} else {
    echo "Will print";
}

You could also insert the Boolean false directly:

<?php
if(false) {
    echo "Will not print";
} else {
    echo "Will print";
}

Though that is rarely done with if statements since it is pretty pointless.