Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Functions
What will be the output of this PHP code?
function add(int $a, int $b) {
    return $a + $b;
}
echo add(5, '3 apples');
ATypeError
B8
C53
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand type coercion in parameters

    Without strict types, PHP converts '3 apples' string to integer 3.
  2. Step 2: Calculate the sum

    5 + 3 = 8, so the function returns 8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    Type coercion without strict types = 8 [OK]
Quick Trick: Strings starting with numbers convert to int without error [OK]
Common Mistakes:
  • Expecting a TypeError without strict types
  • Thinking string concatenation happens
  • Assuming zero is returned on invalid string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes