Bird
0
0

Given the array $data = [0, 1, "", "0", null, true, false, "false"];, which expression returns only the truthy values?

hard📝 Application Q9 of 15
PHP - Variables and Data Types
Given the array $data = [0, 1, "", "0", null, true, false, "false"];, which expression returns only the truthy values?
Aarray_filter($data, fn($v) => $v == true);
Barray_filter($data, fn($v) => $v === true);
Carray_filter($data, fn($v) => (bool)$v);
Darray_filter($data, fn($v) => $v !== false);
Step-by-Step Solution
Solution:
  1. Step 1: Understand truthy values in PHP

    Truthy values are those that convert to true in boolean context: here 1, true, "false".
  2. Step 2: Analyze each option

    B keeps only boolean true; C keeps loose==true (1, true) misses "false"; D keeps falsy extras like "0", null ("0" !== false); A explicit cast keeps exactly [1, true, "false"].
  3. Final Answer:

    Explicit (bool) cast returns only truthy values. -> Option C
  4. Quick Check:

    Use (bool) cast in callback to filter truthy values [OK]
Quick Trick: Use (bool) cast in filter callback for truthy values [OK]
Common Mistakes:
  • Using strict comparison excludes truthy non-boolean
  • Loose == true misses some truthy like "false"
  • Strict !== false keeps some falsy

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes