Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q6 of 15
PHP - Superglobals and Web Context
Identify the error in this PHP code snippet:
$username = $_POST['username'];
$cleanUser = filter_var($username, FILTER_SANITIZE_EMAIL);
if (filter_var($cleanUser, FILTER_VALIDATE_EMAIL)) {
    echo "Valid user email";
} else {
    echo "Invalid email";
}
ANo error, code is correct
BUsing FILTER_SANITIZE_EMAIL on username which may not be an email
CIncorrect function name filter_var
DMissing semicolon after filter_var call
Step-by-Step Solution
Solution:
  1. Step 1: Analyze input and sanitization

    The variable is named username but sanitized as email, which may not be an email format.
  2. Step 2: Understand validation mismatch

    Validating sanitized username as email can cause false invalid results if username is not an email.
  3. Final Answer:

    Using FILTER_SANITIZE_EMAIL on username which may not be an email -> Option B
  4. Quick Check:

    Sanitizing wrong input type causes errors [OK]
Quick Trick: Sanitize and validate matching data types [OK]
Common Mistakes:
  • Sanitizing username as email
  • Assuming filter_var name is wrong
  • Missing semicolons (not in this code)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes