Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - Functions
What will be the output of this PHP code?
function checkAge(?int $age) {
    if ($age === null) {
        return "Age not provided";
    }
    return "Age is $age";
}

echo checkAge(null);
AAge not provided
BAge is
CError: Argument must be int
DAge is null
Step-by-Step Solution
Solution:
  1. Step 1: Understand nullable parameter and null check

    The function accepts an ?int, so null is allowed. It checks if $age === null and returns "Age not provided".
  2. Step 2: Call the function with null

    Calling checkAge(null) triggers the null check, so it returns "Age not provided".
  3. Final Answer:

    Age not provided -> Option A
  4. Quick Check:

    Nullable param null input returns null check message [OK]
Quick Trick: Check for null explicitly when using nullable types [OK]
Common Mistakes:
  • Assuming null converts to empty string
  • Expecting type error on null input
  • Ignoring strict null check with ===

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes