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 PHP Arrays and Control Structures PHP Conditionals Logical Operators

sankyeat kumar
sankyeat kumar
5,468 Points

How 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

In 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
Julian Helmholz
3,242 Points

Emil Pesaxov Not quite correct at the end: = has a higher priority than and. Therefore the true is assigned to $var2.