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 trialJared Skye
3,069 PointsFor this question, why isn't this evaluating to true: $bool = 1; Doesn't a non-zero value evaluate to true?
The question asks to write a boolean that evaluates to true, which from what I am thinking the following should evaluate to true:
$bool = 1;
I remember in the course that zero or empty values evaluate to false and non-zeros or non-empties evaluate true. Am I incorrect in this? The question doesn't say it wants me to set $bool to true it says evaluate to true.
1 Answer
William Li
Courses Plus Student 26,868 PointsFor this part of challenge, you have to do sth like this
<?php
$bool = true;
// or
$bool = (3>2);
?>
Assign value that evaluate to boolean true
to bool.
I remember in the course that zero or empty values evaluate to false and non-zeros or non-empties evaluate true.
that's true, but you have to convert those values to boolean; by writing $bool = 1;
, your bool variable only holds an integer value, but the grader tests whether a value is actually true
, not whether the value evaluates to true
in boolean context.
If you want it to work, you may use the literal cast operator in PHP to convert the integer to boolean.
<?php
$bool = (bool) 1;
?>
That should work.