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

Shakillia Bobo
Shakillia Bobo
2,671 Points

How is '$a' equal to Treehouse?

I answered this question on the compare quiz and I am extremely confused.

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

How is it that despite the variable $a being assigned to Alena, it is still able to echo "Hello Alena"? This is an if statement right? It is saying if the $a is assigned to treehouse to echo said phrase but it's clearly assigned to "Alena". I even tried playing around with this code on PHP sandbox and it seems no matter what value I change the variable for '$a' to, the outcome is still the same. The same happens if I change the value in the if statement. Wo what's the point of using this if no matter what "Hello Alena, Welcome to Treehouse" is always going to be the outcome?

2 Answers

Steven Parker
Steven Parker
231,271 Points

A single "=" symbol is an assignment operator. So this code sets $a to be "Treehouse".

The operator used to check if values are equal is "==" (and happens to be one of the other quiz questions).

And that's why the instructions included this: "TIP: Check the OPERATORS"

Shakillia Bobo
Shakillia Bobo
2,671 Points

So to clarify, even though we have initially assigned $a to "Alena", because we assigned $a to 'Treehouse' in our If statement, then it overrides the first assignment we made. Am I correct on that?