PHP Questions & Answers – PHP Filter

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

Answer: d
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

Answer: b
Explanation: There are two main types of filtering: validation and sanitization.
advertisement
advertisement

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

Answer: a
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?

  1.     <?php
  2.     $num = "123";
  3.     if (!filter_var($num, FILTER_VALIDATE_INT))
  4.         echo("Integer is not valid");
  5.     else
  6.         echo("Integer is valid");
  7.     ?>

a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error
View Answer

Answer: c
Explanation: The function filter_var() can validate and sanitize data. This function filters a single variable with a specified filter.
advertisement

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

Answer: a
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?

advertisement
  1.     <?php
  2.     $var=300;
  3.     $int_options = array("options"=>array ("min_range"=>0, "max_range"=>256));
  4.     if (!filter_var($var, FILTER_VALIDATE_INT, $int_options))
  5.         echo("Integer is not valid");
  6.     else
  7.         echo("Integer is valid");
  8.     ?>

a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error
View Answer

Answer: b
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

Answer: d
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

Answer: a
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?

  1.     <?php
  2.     $value = 'car';
  3.     $result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
  4.     ?>

a) FALSE
b) TRUE
c) NULL
d) ERROR
View Answer

Answer: c
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?

  1.     <?php
  2.     function convertSpace($string)
  3.     {
  4.         return str_replace("_", " ", $string);
  5.     }
  6.     $string = "Peter_is_a_great_guy!";
  7.     echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
  8.     ?>

a) Peter_is_a_great_guy!
b) Peterisagreatguy!
c) Peter is a great guy!
d) Error
View Answer

Answer: c
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.