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 trialKieran Barker
15,028 PointsHow can a variable (that is not an array) have two values?
I don't understand this...
$var1 = true && false;
$var2 = true and false;
var_dump($var1, $var2);
...which prints out:
bool(false)
bool(true)
To me, these lines read:
"I am putting the value 'true' AND the value 'false' inside the variable called 'var1'"
Surely that's impossible? That would be like trying to do this in JavaScript:
var number = 5 && 10;
Isn't that invalid!? Please explain!
1 Answer
João Rosa
2,629 PointsWell, the variable doesn't store two values - it stores the result between the operation (TRUE && FALSE). The results are different because of precedence - "&&" has a greater precedence than "and" - Check PHP Documentation on precedence
So, in the first statement, it acts like ($var1 = (true && false)) - which returns false, cause nothing can be true and false at the same time.
In the second statement, it acts like (($var2 = true) and false) - which returns true, cause it ignores the false part.
Hope I was clear on the explanation.
Kieran Barker
15,028 PointsKieran Barker
15,028 PointsThank you! I think I understand. I didn’t know that could store the result of an operation like that within a variable.