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

What will be displayed in a browser when the following PHP code is executed: - think a answer is wrong

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

8 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The key here lies in the fact that the if statement is using an assignment operator instead of comparison. Take a look:

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

It sets $a = to "Alena", but the very next line inside the if statement, it's not asking if it's equal to "Treehouse", it's actually assigning "Treehouse" to $a. This will be successful and evaluate to true which means that the next echo will happen. At this point "Hello Alena," is echoed out. After that, we have the echo of "Welcome to Treehouse". The end result will be that "Hello Alena, Welcome to Treehouse! will be echoed out. Hope this helps! :sparkles:

Thank you! Now I get it. The 'assigning "Treehouse" to $a' and 'this will be successful' did the trick for me.

I thought the output should be: 'Welcome to Treehouse!' too, but it says the output is: Hello Alena, Welcome to Treehouse! Couldn't understand it and wasn't sure if it was me or the quiz wrong.

I have a screenshot of it, but cannot attach it. It is the 1st quiz in the 'PHP Arrays and Control Structures' course.

Alexander Alegre
Alexander Alegre
14,340 Points

I just ran the code and it did output "Hello Alena, Welcome to Treehouse!" So i may have to re-watch those videos. I googled and it came across this article http://www.quirksmode.org/blog/archives/2008/01/using_the_assig.html so maybe it will help you. It's towards the top so you wont have to read a lot.

That link is about javascript and not php but it should be mostly applicable.

Alexander Alegre
Alexander Alegre
14,340 Points

You're right, it is JS. But language aside it's the same concept. Even though the languages are very different.

You guys the challenge is using a assignment operation not a comparison ?

// Thats the key you coders // Go back and understand the operators they are very important // Then come back answering this Quiz Questions

Alexander Alegre
Alexander Alegre
14,340 Points

Well it seems like you're assigning $a the value of Treehouse so unless there is an else statement there will be no output from the if statement. Since echo "Welcome to Treehouse!" is outside the if statement it will always display that even if the if statement is true. What are the answers from the quiz?

thank you for your help. I'll have a read of the above article :-)

thank you Jennifer, great explanation :-) - have marked best answer.

ismail celik
ismail celik
2,528 Points

thanks J , so we can say everything in () after if is not condition

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Yes, it's still a condition, but it's not checking for the same thing. In PHP when you have an assignment inside a condition, you are asking if what was assigned to the variable was a "truthy" value. The value "Treehouse" would be considered "truthy" as it doesn't fall inside what would be considered "falsey". You can find a more detailed description of truthy vs falsey in this PHP documentation. In this case, because the value being assigned is "truthy" the evaluation of the assignment results in true.

However, if it had checked against an empty string or one of the other "falsey" values, then the evaluation would have been false.

I think a simpler way of putting it would be "=" is to assign values while "==" is for comparison.