Bird
0
0

What is wrong with this PHP code to get a POST value?

medium📝 Debug Q7 of 15
PHP - Superglobals and Web Context
What is wrong with this PHP code to get a POST value?
<?php
if ($_POST['submit']) {
  echo $_POST['email'];
}
?>
AThe if condition should use $_GET instead
BIt should check with isset() before accessing $_POST['submit']
C$_POST['email'] is not a valid variable
DThe echo statement is missing a semicolon
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the if condition

    Accessing $_POST['submit'] without checking if it exists can cause an undefined index error.
  2. Step 2: Correct usage

    Use isset($_POST['submit']) to safely check if the key exists before using it.
  3. Final Answer:

    It should check with isset() before accessing $_POST['submit'] -> Option B
  4. Quick Check:

    Use isset() to avoid undefined index errors [OK]
Quick Trick: Always use isset() before accessing $_POST keys [OK]
Common Mistakes:
  • Directly accessing $_POST keys without isset()
  • Confusing $_POST and $_GET
  • Ignoring PHP notices about undefined indexes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes