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 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,268 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.

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