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 trialsankyeat kumar
5,468 PointsHow can a variable be true or false on its' own?
I don't understand how the following returns a boolean value: var_dump($var1)
How can you test to see if something is true or false if $var one was just a number, for instance?
I would understand if $var1='true', then var_dump($var1) resulted in true but what's with a true or false otherwise?
2 Answers
Ivan Penchev
13,833 PointsWell I can chew this for you, but... this guy wrote it better than me: https://stackoverflow.com/questions/4039542/how-does-php-know-what-type-of-variables-it-uses-or-does-it
Emil Pesaxov
3,684 PointsIn PHP there are certain values that result in a boolean value of FALSE. They are:
the boolean FALSE itself the integer 0 (zero) the float 0.0 (zero) the empty string, and the string "0" an array with zero elements the special type NULL (including unset variables) SimpleXML objects created from empty tags
Everything else is considered TRUE.
In the video when we compared true && false. We received false because both of the arguments must be true. In the second example, we check if $var1 = true and false. According to the order of precedence, true has a higher precedence than the 'and' operator and therefore the value true is assigned to the variable and ignores the rest.
Julian Helmholz
3,242 PointsEmil Pesaxov Not quite correct at the end: = has a higher priority than and. Therefore the true is assigned to $var2.