Bird
0
0

Consider this PHP snippet:

medium📝 Debug Q14 of 15
PHP - Superglobals and Web Context
Consider this PHP snippet:
<?php
print_r($_REQUEST['user']);
?>

If the form sends no 'user' field and the URL has no 'user' parameter, what error will occur and how to fix it?
ANo error; prints empty string
BNotice: Undefined index; fix by checking with isset()
CWarning: Invalid argument; fix by casting to string
DFatal error: Undefined variable; fix by declaring variable
Step-by-Step Solution
Solution:
  1. Step 1: Identify the error from accessing missing key

    Accessing $_REQUEST['user'] when 'user' is not set causes a Notice: Undefined index.
  2. Step 2: Fix by checking if key exists

    Use isset($_REQUEST['user']) before accessing to avoid the notice.
  3. Final Answer:

    Notice: Undefined index; fix by checking with isset() -> Option B
  4. Quick Check:

    Check keys with isset() to avoid notices [OK]
Quick Trick: Always check isset() before accessing $_REQUEST keys [OK]
Common Mistakes:
  • Ignoring undefined index notices
  • Confusing undefined index with undefined variable
  • Assuming no error occurs when key missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes