Bird
0
0

Given the code:

hard📝 Application Q9 of 15
PHP - Type Handling
Given the code:
$data = ['a' => '1', 'b' => '2.5', 'c' => '', 'd' => '0'];
$result = array_map(fn($v) => (bool) $v, $data);
print_r($result);

What is the output?
AError due to invalid casting
B['a' => true, 'b' => true, 'c' => true, 'd' => true]
C['a' => 1, 'b' => 1, 'c' => 0, 'd' => 0]
D['a' => true, 'b' => true, 'c' => false, 'd' => false]
Step-by-Step Solution
Solution:
  1. Step 1: Understand boolean casting rules in PHP

    Empty string and '0' cast to false; non-empty strings cast to true.
  2. Step 2: Apply casting to each array element

    '1' and '2.5' become true; '' and '0' become false.
  3. Final Answer:

    ['a' => true, 'b' => true, 'c' => false, 'd' => false] -> Option D
  4. Quick Check:

    Empty string and '0' cast to false [OK]
Quick Trick: Empty string and '0' cast to false boolean [OK]
Common Mistakes:
  • Assuming all strings cast to true
  • Confusing '0' with true
  • Expecting numeric cast instead of boolean

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes