Bird
0
0

You have a function declared as:

hard📝 Application Q9 of 15
PHP - Functions
You have a function declared as:
function transform(array $data, callable $callback) {
    return array_map($callback, $data);
}

Which of the following calls is valid?
Atransform([1, 2, 3], 'not_a_function');
Btransform([1, 2, 3], null);
Ctransform([1, 2, 3], 123);
Dtransform([1, 2, 3], fn($x) => $x * 2);
Step-by-Step Solution
Solution:
  1. Step 1: Understand callable type declaration

    Parameter declared as callable must be a valid function or closure.
  2. Step 2: Check each call

    transform([1, 2, 3], fn($x) => $x * 2); passes a valid arrow function. transform([1, 2, 3], 'not_a_function'); passes a string not referencing a function. transform([1, 2, 3], 123); passes an integer. transform([1, 2, 3], null); passes null. Only B is valid.
  3. Final Answer:

    transform([1, 2, 3], fn($x) => $x * 2); -> Option D
  4. Quick Check:

    Callable parameter requires valid function or closure [OK]
Quick Trick: Callable parameters accept functions or closures only [OK]
Common Mistakes:
  • Passing invalid strings as callable
  • Passing non-callable types like int or null
  • Confusing callable with array type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes