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 Build a Simple PHP Application Adding a Contact Form Working with Get Variables

Why does Randy use AND instead of &&?

I have taken php courses in the past, and they always use && and I have never seen "and" and "or" being used. Is there a best practice, or does it matter?

And btw, I really love this guy's style of teaching. Very informative, mellow, and easy to follow along.

Thanks!

2 Answers

Hi Colby,

&& & || are the equivalent to and & or

Check this out http://php.net/manual/en/language.operators.logical.php

I guess 'AND' and '&&' are basically the same but with different precedence

'&&' has a higher precedence comparing to 'AND'.

Problem arises when we assign (=) conditional statement to a boolean value as we have the following precedence order: '&&' > '=' > 'AND'

$a = true;
$b = false;

echo '$a && $b: ' . (int) ($a && $b) . "\n"; // 0
echo '$a and $b: ' . (int) ($a and $b). "\n"; // 0

$boolVal_1 = $a && $b; 
echo '$boolVal_1: ' . (int) $boolVal_1 . "\n" ; // 0

// Problem arises since '= ' has a high precedence than 'and'
// The below code is executed implicitly as below:
// ($boolVal_2 = $a) and $b; 
$boolVal_2 = $a and $b; 
echo '$boolVal_2: ' . (int) $boolVal_2 . "\n"; // 1 !!!

// Problem solved when we put parathesis explicitly
$boolVal_3 = ($a and $b); 
echo '$boolVal_3: ' . (int) $boolVal_3 . "\n"; // 0