Bird
0
0

Identify the error in this PHP form handling snippet:

medium📝 Debug Q14 of 15
PHP - Superglobals and Web Context
Identify the error in this PHP form handling snippet:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];
}
echo "Email: $email";
?>
AEchoing variable outside the if block without initialization
BUsing $_POST without checking if 'email' exists
CMissing semicolon after echo statement
DIncorrect request method check
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable scope and initialization

    The variable $email is assigned only inside the if block, so if the request method is not POST, it remains undefined.
  2. Step 2: Check usage of variable outside if block

    Echoing $email outside the if block without initialization causes a notice or error if the form is not submitted.
  3. Final Answer:

    Echoing variable outside the if block without initialization -> Option A
  4. Quick Check:

    Use variable only after initialization [OK]
Quick Trick: Initialize variables before use outside condition [OK]
Common Mistakes:
  • Assuming variables inside if exist outside
  • Not initializing variables before echo
  • Ignoring request method check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes