Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - Type Handling
What will be the output of this PHP code?
function multiply(int $x, int $y): int {
    return $x * $y;
}
echo multiply(3, '4 apples');
A12
BTypeError
C3 4 apples
D7
Step-by-Step Solution
Solution:
  1. Step 1: Understand PHP type coercion with int hints

    PHP converts string '4 apples' to integer 4 when type hinted as int.
  2. Step 2: Calculate the multiplication

    3 * 4 = 12, so the function returns 12 and echoes it.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    String '4 apples' coerced to int 4 = 12 output [OK]
Quick Trick: PHP converts strings starting with numbers to int [OK]
Common Mistakes:
  • Expecting a TypeError for string argument
  • Thinking output is concatenated string
  • Adding numbers instead of multiplying

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes