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 Compare

Kat Cuccia
Kat Cuccia
786 Points

This is not an if-else statement, so it should display what the final echo says, but it said that was incorrect?

The question asked me to pick what it would display if the following code was executed.

<?php 
$a = "Alena"; 
if ($a = "Treehouse") { 
echo "Hello Alena, "; 
} 
echo "Welcome to Treehouse!"; 
?>

Given that the second echo command is not in an if-else statement, it would be run regardless of the if statement was true or not. Even if there was an else after the if statement, it would still display "Welcome to Treehouse!", because $a = "Treehouse" still wouldn't be true. Am I wrong? Am I missing something? Is this question glitched? Is there supposed to be an else to this if statement? (I answered that it would echo "Welcome to Treehouse!" because by all accounts, it should? But maybe I'm wrong)

Edit: is.. is it setting the variable within the if statement?????? Is the $a = "Alena" line just meant to throw people off????? What in the world???????

1 Answer

andren
andren
28,558 Points

This question is related to the other question you posted earlier, which I have left an answer for.

You are entirely right that "Welcome to Treehouse!" will be printed out regardless of what happens in the if statement, however that is not the only thing that will be printed out.

Both of the echo statements will actually run, so the answer to the Quiz is: "Hello Alena, Welcome to Treehouse!"

The reason is a bit complicated. First of all you should note that in the if statement $a is not being compared to Treehouse as you might expect, they actually use = not ==. That is an intentional mistake.

When you use an assignment as a condition it actually results in the value that is assigned being compared. So $a gets set to "Treehouse". And then the if statement evaluates whether the value "Treehouse" is true or false.

As I explained in my answer to your previous question PHP will convert certain values to true and some to false based on whether they fit into a list of "Falsy" values. "Treehouse" is not a value that fits within the "falsy" value list, therefore it is considered to be true.

And thus the if statement runs.

Kat Cuccia
Kat Cuccia
786 Points

That's... both absurd and infuriating, especially given that it was a question asked before the logic was explained (in the next course). Thank you anyway.

andren
andren
28,558 Points

Oh I won't disagree with you there. = vs == is not a difference that's easy to spot in the first place, and the result of that mistake is not obvious at all.

It's a question that might throw even experienced PHP developers off track for a moment. So to expect beginners to be able to answer it this early in the track is indeed absurd.