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 trialFrank D
14,042 PointsWhy 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?
2 Answers
Zachary Green
16,359 PointsIts because in php aNd most other programming languages = is the assign operator and == is the equality operator.
= sets values == test equality
Frank D
14,042 PointsThank you, that make sense now!
Gianluca Mauro
3,236 PointsYou 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.
Greg Kitchin
31,522 PointsGreg Kitchin
31,522 PointsI'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.