Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,520 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.