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

Thomas Minnefor
Thomas Minnefor
7,952 Points

quiz question

Hi - if Alena is assigned to $a the conditional evaluates to false - no?

What will be displayed in a browser when the following PHP code is executed:

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

Thomas Minnefor
Thomas Minnefor
7,952 Points

I think I see it now -

The single = is assignment. For the test to occur double == to check equality would be needed. So, it appears this is actually poor syntax - assignment wouldn't typically occur in a conditional test.

Thanks again. Stepping through each line helps.

Tracy,

Thanks. I'll need to go through PHP docs. Seems strange syntax that in the conditional Treehouse is being assigned to $a instead just a test of true/false. Seems I missed this distinction in the lesson video too. Will view it again.

Thanks for stepping through the code.

I'm glad it makes sense. It was my first reply for help when I happened to be on the same course!

Sometimes, you can use it as a conditional when you want to know if something is set to that value., then one thing can happen. Kinda weird here though, but it just may be a common mistake when you really do intend to compare and get unexpected and crazy output. It seems that quiz questions/projects may be just prepping us for real world stuff. And my guess is that that kinda mistake happens a lot.

Now, neither of us will (probably) ever forget!

Good luck!

1 Answer

Hi Thomas, Check out the difference between: a comparison == vs. an assignment =

$a is actually getting reassigned to be "Treehouse" and "Hello Alena" gets printed now as you go through the code. And then you continue through and the "Welcome to Treehouse!" is the next step.

I though of it like this as I read through it:

  1. $a is Alena,
  2. if $a is Treehouse now (it is since it was just assigned to $a in that statement... the tricky part because it is not being compared it is being assigned. So it is True... proceed through curly braces)
  3. echo "Hello Alena"
  4. echo "Welcome to Treehouse"

hope it made sense.