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

Adrian Tanta
Adrian Tanta
467 Points

hello please check this quiz the question number one is not correct in my opionion, D is the correct answer.

hello please check this quiz the question number one is not correct in my opionion, D is the correct answer.

Conor Redmond
Conor Redmond
5,375 Points

Hi Adrian,

The == checks if the two values are equal, but === checks if they are identical in every way including variable type!

Thanks

Conor

Adrian Tanta
Adrian Tanta
467 Points

ok it should anyway print "Welcome to Treehouse! , we are outside the if condition here, right? I also think you have a typo, it shoud be " if ($a == "Treehouse") ... " original quiz code:

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

1 Answer

andren
andren
28,558 Points

Treehouse quizzes are randomized each time you take them, so the order that the questions appear and the order that the answer appear in is not consistent. Therefore quiz one and option D is not actually very descriptive.

However I assume that the question that confuses you is the quiz that contains this code:

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

Since that tends to be the question that trips up most people from this quiz, due to the fact that it's honestly pretty complex for where in the course it is placed.

The above code will produce the text "Hello Alena, Welcome to Treehouse!" you can verify that yourself by running it on your own.

The reason why that happens is actually fairly complex, and would likely be difficult to understand at the stage in the PHP course you are at.

But to sum it up very shortly and somewhat simplified, PHP will look at the value that is being assigned "Treehouse" (= is used for assignment, rather than comparison) and then convert that to a Boolean (true or false) value since that is what the if statement is expecting to receive. Non-empty strings are considered as "truthy" values in PHP so it will convert it to true which will make the if statement run.

This question is likely included in the quiz in order to show that a small mistake (using = instead of ==) can cause quite unexpected behavior, so it is something you should be on the outlook for.