Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Functions Introducing Functions Introducing Functions

Øyvind-Andreas Emanuelsen
Øyvind-Andreas Emanuelsen
2,867 Points

When 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.

Ladislav Vysmek
Ladislav Vysmek
1,666 Points

Great answer, I had the same. :-)

I 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
Jeff Lemay
14,266 Points

A 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.

$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?!";