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 trialChris Seals
4,507 PointsTrouble 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
28,558 PointsTo 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
Treehouse TeacherJennifer Nordell
Treehouse TeacherI'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!
Chris Seals
4,507 PointsChris Seals
4,507 PointsSo 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
28,558 Pointsandren
28,558 PointsJennifer 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 booleantrue
or not. In PHP certain values are considered inherently "falsey" meaning that they are converted tofalse
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 totrue
. Which does make theif
statement run.