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

Chris Seals
Chris Seals
4,507 Points

Trouble with this if statement.

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

How is it that in the if statement "if ($a = Treehouse) {echo 'hello... whatever';}" it can redefine the value of $a (I understand the difference between "=" and "==" operators) within the if statement, and seemingly ignore the if statement set up and simply run: the redefining statement and the echo command?

1 Answer

andren
andren
28,558 Points

To be clear, the thing the if is now evaluating is the success of the reassignment of the value in the $a variable.

I don't like to correct you Jennifer Nordell, but I feel obligated to point out that this statement is not actually correct.

When an assignment/reassignment is evaluated what is tested is the value that is being assigned, not whether or not the assignment is a success.

In other words the string "Treehouse" is what actually ends up being evaluated by the if statement. Which evaluates to true due to non-empty strings being considered truethy values in PHP.

If you changed the assignment to something that would be considered a falsey value like this:

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

Then the if statement would not run. Even though there would not be any problems with the actual reassignment of the value.

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

I'm removing my answer and changing your comment to an answer. You are indeed correct. I was thinking it worked like it did back in ANSI C where my answer would have been correct. My apologies! :sparkles:

Chris Seals
Chris Seals
4,507 Points

So if I understand correctly - with nothing else to define the if statement, other than a value which will evaluate to be true, the (Apologies if this is not the correct terminology) "then" statement (Im considering this to be whats in the curly braces) will run?

andren
andren
28,558 Points

Jennifer Nordell: No worries, I've certainly mixed up knowledge from on language while writing about another before, so I don't blame you.

Chris Seals: Sorry about my answer being somewhat confusing, as you might be able to tell I didn't write it with the intention of it being set as an answer. The answer Jennifer Nordell originally wrote had a bit more details in it about your question.

But the answer is yes. if statements run based on whether the value they are given evaluates to the boolean true or not. In PHP certain values are considered inherently "falsey" meaning that they are converted to false when converted to a boolean. There are a quite a few of those values but they include things like 0, "0", "" and empty things in general.

Any value that is not considered "falsey" is automatically considered "truethy" which means that they are converted to true. Since "Treehouse" is not a "falsey" value it evalutes to true. Which does make the if statement run.