Bird
0
0

Which of the following is the correct syntax to use array_filter with a callback function named isPositive?

easy📝 Syntax Q3 of 15
PHP - Array Functions
Which of the following is the correct syntax to use array_filter with a callback function named isPositive?
A$filtered = array_filter($arr, isPositive());
B$filtered = array_filter('isPositive', $arr);
C$filtered = array_filter($arr, 'isPositive');
D$filtered = array_filter($arr, isPositive);
Step-by-Step Solution
Solution:
  1. Step 1: Recall array_filter syntax

    The correct syntax is array_filter(array, callback), where callback is a string with function name.
  2. Step 2: Analyze options

    $filtered = array_filter($arr, 'isPositive'); matches the syntax. $filtered = array_filter('isPositive', $arr); reverses parameters. $filtered = array_filter($arr, isPositive()); calls the function instead of passing it. $filtered = array_filter($arr, isPositive); misses quotes around callback.
  3. Final Answer:

    $filtered = array_filter($arr, 'isPositive'); -> Option C
  4. Quick Check:

    Correct syntax = array_filter(array, 'callback') [OK]
Quick Trick: Pass callback as string without parentheses [OK]
Common Mistakes:
  • Swapping parameters
  • Calling callback instead of passing
  • Omitting quotes around callback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes