1. What is the primary purpose of PHP filters?
a) To modify the output of PHP scripts
b) To sanitize and validate data from external sources
c) To filter the contents of a file
d) To apply CSS filters in PHP
Answer:
b) To sanitize and validate data from external sources
Explanation:
PHP filters are primarily used to sanitize and validate data coming from insecure sources, such as user input.
2. Which function is used to filter a single variable with a specified filter in PHP?
a) filter_var()
b) filter_input()
c) filter_process()
d) var_filter()
Answer:
a) filter_var()
Explanation:
The filter_var() function is used to filter a single variable with a specified filter.
3. What does the FILTER_SANITIZE_STRING filter do in PHP?
a) Validates whether the data is a string
b) Sanitizes a string by removing tags
c) Encrypts a string
d) Converts a string to an array
Answer:
b) Sanitizes a string by removing tags
Explanation:
FILTER_SANITIZE_STRING removes all HTML tags from a string, effectively sanitizing it.
4. Which filter in PHP is used to validate email addresses?
a) FILTER_VALIDATE_EMAIL
b) FILTER_SANITIZE_EMAIL
c) FILTER_EMAIL
d) EMAIL_FILTER
Answer:
a) FILTER_VALIDATE_EMAIL
Explanation:
FILTER_VALIDATE_EMAIL is used to validate whether the given value is a valid email address.
5. How do you check if a variable is an integer using PHP filters?
a) Using FILTER_VALIDATE_INT
b) Using FILTER_SANITIZE_NUMBER_INT
c) Using INT_FILTER
d) Using VALIDATE_INT
Answer:
a) Using FILTER_VALIDATE_INT
Explanation:
FILTER_VALIDATE_INT is used to validate whether a variable is an integer.
6. What does the filter_input() function do in PHP?
a) Filters a variable from a script
b) Gets an external variable and optionally filters it
c) Filters all input data
d) Validates input data
Answer:
b) Gets an external variable and optionally filters it
Explanation:
The filter_input() function is used to get an external variable like data from a form and optionally filters it.
7. Which filter in PHP is used to sanitize a URL?
a) FILTER_SANITIZE_URL
b) FILTER_VALIDATE_URL
c) URL_FILTER
d) SANITIZE_URL
Answer:
a) FILTER_SANITIZE_URL
Explanation:
FILTER_SANITIZE_URL is used to sanitize a URL by removing all illegal URL characters.
8. What is the purpose of the FILTER_FLAG_NO_ENCODE_QUOTES flag in PHP?
a) To prevent encoding of quotes
b) To encode quotes
c) To remove quotes
d) To add quotes
Answer:
a) To prevent encoding of quotes
Explanation:
When used with certain filters, FILTER_FLAG_NO_ENCODE_QUOTES prevents the encoding of quotation marks.
9. How do you validate an IP address in PHP?
a) Using FILTER_VALIDATE_IP
b) Using FILTER_SANITIZE_IP
c) Using IP_VALIDATE
d) Using VALIDATE_IP
Answer:
a) Using FILTER_VALIDATE_IP
Explanation:
FILTER_VALIDATE_IP is used to validate if a value is a valid IP address.
10. What does the FILTER_SANITIZE_NUMBER_FLOAT filter do in PHP?
a) Validates a floating-point number
b) Sanitizes a number by removing illegal characters
c) Converts a string to a floating-point number
d) Rounds a floating-point number
Answer:
b) Sanitizes a number by removing illegal characters
Explanation:
FILTER_SANITIZE_NUMBER_FLOAT removes all illegal characters from a number, leaving only digits, +- and optionally .,eE.