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 Basics PHP on the Web Final Quiz

Kat Cuccia
Kat Cuccia
786 Points

Why does a variable assigned one double quote test as a false boolean value?

One of the questions in the final quiz asks, "Which of the following variables would be considered FALSE when tested as a boolean?" The correct answer is $name = ";. Why on earth does a " result in false??

2 Answers

andren
andren
28,558 Points

Because PHP (as well as many other languages) has certain values they consider "Truthy" and some which they consider "Falsy". As you might guess "Truthy" values are converted to the boolean true and "Falsy" are converted to false.

In the case of PHP there is a set list of values that are considered "Falsy", and any value that is not in that list are considered "truthy". Here is the list of "Falsy" values:

  • 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

Since $name is set to an empty string in the Quiz it is considered false.

good answer, thank you