200-710 · Question #104
What is the output of the following code? var_dump (boolval (new StdClass()));
The correct answer is A. bool(true). In PHP, any object instance is always truthy when converted to boolean - this is a core type-juggling rule. new StdClass() creates a valid object (even though it's empty), and boolval() converts it to bool(true), which var_dump() then prints as bool(true). Option B…
Question
Options
- Abool(true)
- Bbool(false)
How the community answered
(17 responses)- A71% (12)
- B29% (5)
Explanation
In PHP, any object instance is always truthy when converted to boolean - this is a core type-juggling rule. new StdClass() creates a valid object (even though it's empty), and boolval() converts it to bool(true), which var_dump() then prints as bool(true).
Option B (bool(false)) is wrong because PHP's falsy values are a fixed set of primitives: null, 0, 0.0, "", "0", [], and false itself - objects are never in that set, regardless of whether they have properties.
Memory tip: Think "objects exist, therefore they're true." Only nothing (null/empty/zero) is false in PHP - a living object always evaluates to true.
Topics
Community Discussion
No community discussion yet for this question.