Bird
0
0

You have an array of mixed values: ["10", 20, "30.5", true]. Which PHP code correctly casts all elements to integers using type casting syntax?

hard📝 Application Q8 of 15
PHP - Type Handling
You have an array of mixed values: ["10", 20, "30.5", true]. Which PHP code correctly casts all elements to integers using type casting syntax?
A$ints = array_map(fn($v) => int $v, ["10", 20, "30.5", true]);
B$ints = array_map(fn($v) => int($v), ["10", 20, "30.5", true]);
C$ints = array_map(fn($v) => (integer) $v, ["10", 20, "30.5", true]);
D$ints = array_map('int', ["10", 20, "30.5", true]);
Step-by-Step Solution
Solution:
  1. Step 1: Understand casting each element in array

    Use array_map with a function casting each element to integer using (integer) or (int).
  2. Step 2: Evaluate options

    $ints = array_map(fn($v) => int $v, ["10", 20, "30.5", true]); uses int $v without parens, invalid syntax. $ints = array_map(fn($v) => (integer) $v, ["10", 20, "30.5", true]); uses (integer), correct. $ints = array_map('int', ["10", 20, "30.5", true]); uses 'int' as string, invalid. $ints = array_map(fn($v) => int($v), ["10", 20, "30.5", true]); uses int() function, invalid.
  3. Step 3: Choose best answer

    $ints = array_map(fn($v) => (integer) $v, ["10", 20, "30.5", true]); is correct and uses valid syntax with (integer).
  4. Final Answer:

    $ints = array_map(fn($v) => (integer) $v, ["10", 20, "30.5", true]); -> Option C
  5. Quick Check:

    Use array_map with (integer) casting function [OK]
Quick Trick: Use array_map with (int) or (integer) casting [OK]
Common Mistakes:
  • Using 'int' as function
  • Using int() function
  • Not casting inside callback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes