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Øyvind-Andreas Emanuelsen
2,867 PointsWhen to use a single = and when to use a ==? To me they seem used interchangeably and can't find any explanation online.
I can't find any explanation on when you use the the single =. I understand the purpose and meaning of == and === but the rules for when to use the single = when defining something is unclear.
Mike Beljak
555 PointsI wonder if a good way of explaining it might be:
The single equal symbol (=) assigns a value to a variable. In many cases, that assigned value is human-made, or in other words, arbitrarily described. There is a sense of great freedom here.
The double equal symbol (==) questions the equality of two things. Importantly, and inherently, it asks: "Is it TRUE that these two things joined by the symbol (==) are equal?". It might be best described as a "logical operation". It is mathematical. In a sense there is nothing arbitrary about the use of this symbol. In short, it is what it is :-).
Just some thoughts to clear my head in the early stages of learning PHP!
1 Answer
Jeff Lemay
14,268 PointsA single equal sign is used to set a variable equal to something. A double equal sign is used to determine if something is equal to something.
<?php
$name = "Jeff";
if ( $name == "Jeff" ) {
echo "Damn, that guy's cool.";
} elseif ( $name != "Jeff" ) {
echo "Wait, you're not Jeff!";
} else {
echo "Who are you?!";
}
?>
Øyvind-Andreas Emanuelsen
2,867 PointsThanks! That was lightning fast and informative!
Jeff Lemay
14,268 PointsYou're welcome!
Ladislav Vysmek
1,666 PointsLadislav Vysmek
1,666 PointsGreat answer, I had the same. :-)