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
201,932 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?