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 trialKatie Taira
2,836 PointsI 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
Treehouse TeacherHi there, Katie Taira ! I would bet that you understand conditionals just fine 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!
Katie Taira
2,836 PointsKatie Taira
2,836 PointsAfter I posted this question I figured out the answer but thank you for answering promptly! <3