200-710 · Question #102
What is the output of the following code? var_dump (boolval (-1));
The correct answer is A. bool(true). boolval(-1) returns true because PHP converts any non-zero integer to true - the sign doesn't matter. Only 0 (and equivalent falsy values like "", null, []) convert to false, making A correct. Why B is wrong: false would only result from boolval(0), not boolval(-1). A common…
Question
Options
- Abool(true)
- Bbool(false)
How the community answered
(64 responses)- A84% (54)
- B16% (10)
Explanation
boolval(-1) returns true because PHP converts any non-zero integer to true - the sign doesn't matter. Only 0 (and equivalent falsy values like "", null, []) convert to false, making A correct.
Why B is wrong: false would only result from boolval(0), not boolval(-1). A common misconception is that negative numbers are "false-like," but PHP's truthiness rule is simple: zero = false, everything else = true.
Memory tip: Think of it like a light switch - any non-zero value means "the light is on" (true). Only 0 turns the light off (false). Negative numbers are just as "on" as positive ones.
Topics
Community Discussion
No community discussion yet for this question.