Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Array Functions
What will be the output of this PHP code?
$arr = ['a' => 0, 'b' => 1, 'c' => false, 'd' => true];
$result = array_filter($arr);
print_r($result);
AArray ( [a] => 0 [c] => false )
BArray ( [b] => 1 [d] => true )
CArray ( [b] => 1 [d] => 1 )
DArray ( [a] => 0 [b] => 1 [c] => false [d] => true )
Step-by-Step Solution
Solution:
  1. Step 1: Understand default behavior of array_filter

    Without a callback, it removes elements that are falsey (0, false, null, '').
  2. Step 2: Identify which elements remain

    Elements with values 1 and true are truthy and remain; keys 'b' and 'd'.
  3. Final Answer:

    Array ( [b] => 1 [d] => true ) -> Option B
  4. Quick Check:

    Default array_filter removes falsey values [OK]
Quick Trick: Default array_filter removes falsey values [OK]
Common Mistakes:
  • Including zero or false
  • Confusing true with 1
  • Expecting all elements to remain

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes