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

Katie Taira
Katie Taira
2,836 Points

I don't understand this conditional

What will be displayed?

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

I answered "Welcome to treehouse!" but according to the quiz it is wrong and "Hello Alena, welcome to Treehouse!" is correct. In my understanding since $a is Alena and not Treehouse the if statement is false ergo only Welcome to tree house is displayed. Unless I'm not understanding something about conditionals here... Thank you.

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Katie Taira ! I would bet that you understand conditionals just fine :smiley: If it's any consolation, this one fooled me the first time I looked at it, too.

The key lies in this line:

if ($a = "Treehouse")

That is not a comparison. That is an assignment. Remember that a single equals sign assigns a value. A double equals sign == asks if it is equal. When we have an assignment like this, if whatever is on the right side of the equals sign is "truthy" then the entire evaluation is considered true. A non-empty string is "truthy".

Now, if the question had been:

if ($a == "Treehouse")

... then your answer would likely be spot on!

Hope this helps! :sparkles:

Katie Taira
Katie Taira
2,836 Points

After I posted this question I figured out the answer but thank you for answering promptly! <3