Bird
0
0

Consider this PHP snippet:

medium📝 Predict Output Q5 of 15
PHP - Superglobals and Web Context
Consider this PHP snippet:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $age = $_POST['age'] ?? 'unknown';
  echo "Age: $age";
} else {
  echo 'No data submitted';
}
?>

What will be the output if the form is submitted without the 'age' field?
AError: Undefined index
BAge: unknown
CNo data submitted
DAge:
Step-by-Step Solution
Solution:
  1. Step 1: Check if form is submitted via POST

    Since the form is submitted, the condition is true.
  2. Step 2: Use null coalescing operator

    $_POST['age'] is missing, so $age gets 'unknown'.
  3. Final Answer:

    Age: unknown -> Option B
  4. Quick Check:

    Missing field uses default with ?? operator [OK]
Quick Trick: Use ?? to provide default if POST field missing [OK]
Common Mistakes:
  • Expecting empty string instead of default
  • Not using ?? operator
  • Ignoring missing field errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes