PHP Questions & Answers – PHP Filter

PHP Multiple Choice Questions & Answers (MCQs) focuses on “PHP Filter”.

Q 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)

Show Answer Answer:-D. i), ii), iii) and iv)
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.


Q 2. How many types of filtering are present in PHP?
A. 3
B. 2
C. 4
D. None

Show Answer Answer:-B. 2
Explanation There are two main types of filtering: validation and sanitization.


Q 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

Show Answer Answer:-A. filter_var_array()


Q 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

Show Answer Answer:-C. Integer is valid
Explanation The function filter_var() can validate and sanitize data. This function filters a single variable with a specified filter.


Q 5. Which one of the following does not describe a validating filter?
A. Strict format rules
B. Are used to validate user input
C. Are used to allow or disallow specific characters in a string
D. Returns the expected type on success or FALSE on failure

Show Answer Answer:-C. Are used to allow or disallow specific characters in a string
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.


Q 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 valid
C. Integer is not valid
D. Error

Show Answer Answer:-C. Integer is not valid
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”.


Q 8. Which one of the following filter checks if the variable of the specified type exists?
A. filter_var
B. filter_has_var
C. filter_id
D. filter_var_array

Show Answer Answer:-B. filter_has_var
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.


Q 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. NULL
B. TRUE
C. FALSE
D. ERROR

Show Answer Answer:-A. NULL
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.


Q 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

Show Answer Answer:-C. Peter is a great guy!
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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page