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

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Always using && and || rather than AND or OR?

Would I be wrong to say that it's safer to always use && and || rather than AND or OR so we avoid weird behaviours like the one Alena pointed out in the end of the video?

1 Answer

Ivan Penchev
Ivan Penchev
13,833 Points

Not always. Its important to understand that precedence differs between && and AND (&& has higher precedence than and), something that causes confusion when combined with a ternary operator. For instance,

if ($var = true && false) // Compare true with false and assign to $var
if ($var = true and false) // Assign true to $var and compare $var to false

Same goes for OR too.

So now that you understand that, you know where you can use them. Sometimes AND and OR do create better readibility of our code.