This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “PHP Filter”.
1. Which of the following is/are an external data?
i) Cookies ii) Input data from a form iii) Server Variables iv) Web services data
a) Only ii)
b) ii) and iii)
c) Only iii)
d) i), ii), iii) and iv)
View Answer
Explanation: The web applications receives external input. External input/data can be: User input from a form, Cookies, Web services data, Server variables and, Database query results.
2. How many types of filtering are present in PHP?
a) 3
b) 2
c) 4
d) None
View Answer
Explanation: There are two main types of filtering: validation and sanitization.
3. Which one of the following filter is used to filter several variables with the same or different filters?
a) filter_var_array()
b) filter_var()
c) filter_input
d) filter_input_array
View Answer
Explanation: The function filter_var_array() can get multiple variables and it optionally filters them. The function is useful for filtering many values without calling filter_var().
4. What will be the output of the following PHP code?
<?php
$num = "123";
if (!filter_var($num, FILTER_VALIDATE_INT))
echo("Integer is not valid");
else
echo("Integer is valid");
?>
a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error
View Answer
Explanation: The function filter_var() can validate and sanitize data. This function filters a single variable with a specified filter.
5. Which one of the following does not describe a validating filter?
a) Are used to allow or disallow specific characters in a string
b) Are used to validate user input
c) Strict format rules
d) Returns the expected type on success or FALSE on failure
View Answer
Explanation: Validate filter are used to validate user input, it have strict format rules and it returns the expected type on success or FALSE on failure but ‘are used to allow or disallow specific characters in a string’ describes sanitizing filters.
6. What will be the output of the following PHP code?
<?php
$var=300;
$int_options = array("options"=>array ("min_range"=>0, "max_range"=>256));
if (!filter_var($var, FILTER_VALIDATE_INT, $int_options))
echo("Integer is not valid");
else
echo("Integer is valid");
?>
a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error
View Answer
Explanation: Since the integer is “300” it is not in the specified range, and the output of the code above will be: “Integer is not valid”.
7. If the input variable is a string like this “http://www.saåånfoøøundry.com/”, the $url variable after the sanitizing will look like?
a) http://www.saåånfoøøundry.com/
b) http://www.saaanfoooundry.com/
c) http://www.saånfoøundry.com/
d) https://www.sanfoundry.com/
View Answer
Explanation: Sanitize is nothing but take away invalid characters so therefore the invalid characters like å and ø will be removed.
8. Which one of the following filter checks if the variable of the specified type exists?
a) filter_has_var
b) filter_var
c) filter_id
d) filter_var_array
View Answer
Explanation: The filter filter_has_var checks if the variable of the specified type exists. Whereas the function filter_id() returns filter ID of a specified filter name. The function filter_var() can validate and sanitize data. The function filter_var_array() can get multiple variables and it optionally filters them.
9. What will be the output of the following PHP code?
<?php
$value = 'car';
$result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
?>
a) FALSE
b) TRUE
c) NULL
d) ERROR
View Answer
Explanation: There is an undocumented filter flag for FILTER_VALIDATE_BOOLEAN. The documentation implies that it will return NULL if the value doesn’t match the allowed true/false values. However this doesn’t happen unless you give it the FILTER_NULL_ON_FAILURE flag.
10. What will be the output of the following PHP code?
<?php
function convertSpace($string)
{
return str_replace("_", " ", $string);
}
$string = "Peter_is_a_great_guy!";
echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
?>
a) Peter_is_a_great_guy!
b) Peterisagreatguy!
c) Peter is a great guy!
d) Error
View Answer
Explanation: The code above converts all “_” to white spaces. Call the filter_var() function with the FILTER_CALLBACK filter and an array containing our function.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check MCA Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Information Technology Books
- Check PHP Books