Bird
0
0

Which approach correctly uses superglobals to achieve this?

hard📝 Application Q8 of 15
PHP - Superglobals and Web Context
You want to write a PHP function that returns the value of a form field named 'username' regardless of whether the form was submitted via GET or POST. Which approach correctly uses superglobals to achieve this?
AOnly check $_GET for 'username'
BOnly check $_POST for 'username'
CCheck if 'username' exists in $_POST, else check $_GET
DUse $_REQUEST['username'] without checks
Step-by-Step Solution
Solution:
  1. Step 1: Understand form submission methods and check $_POST first, then $_GET if not set

    Forms can submit data via GET or POST, so both superglobals may hold the data. This ensures you get the value regardless of method, avoiding undefined index errors.
  2. Final Answer:

    Check if 'username' exists in $_POST, else check $_GET -> Option C
  3. Quick Check:

    Check $_POST then $_GET for form data [OK]
Quick Trick: Check $_POST first, then $_GET for form inputs [OK]
Common Mistakes:
  • Assuming data is only in one superglobal
  • Using $_REQUEST without validation
  • Not checking if keys exist before access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes