A. Using FILTER_SANITIZE_NUMBER_INT instead of FILTER_VALIDATE_INT
B. Missing semicolon after echo statements
C. Incorrect variable name in filter_var
D. No error, code is correct
Solution
Step 1: Check filter used for validation
FILTER_SANITIZE_NUMBER_INT cleans input but does not validate if it's a proper integer.
Step 2: Correct filter for validation
FILTER_VALIDATE_INT should be used to check if input is a valid integer.
Final Answer:
Using FILTER_SANITIZE_NUMBER_INT instead of FILTER_VALIDATE_INT -> Option A
Quick Check:
Validation needs FILTER_VALIDATE_INT, not sanitize [OK]
Hint: Validation uses FILTER_VALIDATE_INT, not sanitize filters [OK]
Common Mistakes:
Confusing sanitize and validate filters
Ignoring filter_var second argument importance
Assuming sanitize also validates
5. You want to accept a username input that must be alphanumeric and between 5 to 10 characters. Which approach correctly combines validation and sanitization in PHP?
hard
A. Validate with FILTER_VALIDATE_REGEXP for pattern, then sanitize with FILTER_SANITIZE_STRING
B. Sanitize with FILTER_SANITIZE_STRING, then validate length and ctype_alnum
C. Only sanitize with FILTER_SANITIZE_STRING, no validation needed
D. Only validate length with strlen, ignore sanitization
Solution
Step 1: Understand validation and sanitization order
First sanitize the input to remove unwanted characters using FILTER_SANITIZE_STRING.
Step 2: Validate length and alphanumeric
Then validate the length with strlen and check if the string is alphanumeric using ctype_alnum.
Final Answer:
Sanitize with FILTER_SANITIZE_STRING, then validate length and ctype_alnum -> Option B
Quick Check:
Sanitize first, then validate pattern and length [OK]
Hint: Sanitize first, then validate length and alphanumeric [OK]
Common Mistakes:
Validating before sanitizing
Skipping sanitization and trusting validation only
Ignoring length check in validation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.