Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Functions
What will be the output of this PHP code?
function multiply(?int $a, int $b) {
    return ($a ?? 1) * $b;
}
echo multiply(null, 5);
A0
B5
CError: Argument 1 must be int or null
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Understand nullable parameter and null coalescing

    The first parameter can be int or null. If null, $a ?? 1 evaluates to 1.
  2. Step 2: Calculate the return value

    Since $a is null, it becomes 1, multiplied by 5 equals 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Nullable with ?? provides fallback value [OK]
Quick Trick: Use ?? to replace null with default value [OK]
Common Mistakes:
  • Expecting error on null input
  • Assuming null multiplied by int is null
  • Confusing default parameter with nullable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes