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 Basics (Retired) PHP Conditionals & Loops Conditionals

Frank D
Frank D
14,042 Points

Why using == and not =

Why we need to use the == (identical) operator and not just the = (equal) one in this example?

<?php

define("USE_FULL_NAME", TRUE);

if( "USE_FULL_NAME" == TRUE) {

      // codetorun
}

?>

I've tried using the = and gave me an error, but I don't get why?

Greg Kitchin
Greg Kitchin
31,522 Points

I've not done much PHP, but my understanding of different programming languages, there's a difference between =, and ==. = is typically used to assign values, while == is used to compare values.

For example, in Java, int madeUpNumber = 1;

== is used to compare items, either strings or numbers.
So lets say int x = 1; int y = 1;

return boolean(x == y); Not the best code, but the above would return true, as x and y have the same values.

2 Answers

Zachary Green
Zachary Green
16,359 Points

Its because in php aNd most other programming languages = is the assign operator and == is the equality operator.

= sets values == test equality

Frank D
Frank D
14,042 Points

Thank you, that make sense now!

Gianluca Mauro
Gianluca Mauro
3,236 Points

You can also use if( USE_FULL_NAME ) for test if the constant USE_FULL_NAME is true, or if( ! USE_FULL_NAME) for test if it is false. this works but code is less readable.