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 trialjhdsfjhdfs hjkhkjkhj
Courses Plus Student 2,980 PointsAt 3:20 Randy used the comparison operator AND. I used the ampersands &&. Which is better?
I've always used && ampersands, I've never seen AND. I was wondering what team treehouse's professional opinion on this is.
2 Answers
Ricardo Hill-Henry
38,442 PointsChris Southam I do believe there's the possibility false assignment when using AND as opposed to &&. AND has a lower precedence than &&. So, when having a conditional
<?php $x && $y || $z; ?>
<?php $x AND $y || $z; ?>
In the latter, we're actually executing x and (y or z) since || has a higher precedence then AND.
Now you may say, why not just swap || with OR. Well here's another example of this failing
<?php
$x = true;
$y = false;
$seekingTheTruth = $x and $y;
?>
In this example we can expect the value to return true. This is because the equal operator has higher precedence: ($seekingTheTruth = $x) and $y;
Chris Southam
1,823 PointsEither, or - which ever you find easier to read as long as you are consistent in your code.
&& makes a lot of sense for AND but || is a little harder to understand for OR. You need to use both consistently, so choose a version and stick to it!
jhdsfjhdfs hjkhkjkhj
Courses Plus Student 2,980 PointsThanks Chris. I'm going to stick with the &&, || operators. Just needed a second opinion.
jhdsfjhdfs hjkhkjkhj
Courses Plus Student 2,980 Pointsjhdsfjhdfs hjkhkjkhj
Courses Plus Student 2,980 Points$seekingTheTruth = $x && $y;
returns TRUE, which is what I favor. Thanks Mr. Cuckoo