php – Are there any advantages to using the filter_input function instead of isset?

Question:

I've always been used to using isset to check if an external variable is filled and if you are using it.

Example:

if(isset($_GET['pag']))
     $pag = $_GET['pag'];

Now I've installed NetBeans and it gives me a Warning telling me to use the filter_input function.

Example:

if($pag = filter_input(INPUT_GET , 'pag'));

Are there any advantages to using the filter_input function?

Answer:

As it can be read in the documentation of the filter_input function (English) :

filter_input — Gets a specific external variable by name and optionally filters it

Which translated:

filter_input — Gets a specific external variable by name and optionally filters its value.

The advantage of using the filter_input function is the fact that not only will we collect the value, but we can also filter it during this collection:

$search_html = filter_input(INPUT_GET, 'chavePesquisa', FILTER_SANITIZE_SPECIAL_CHARS);

Useful to protect, clean or validate user data without using multiple functions and several lines of code.

The list of filters can be found here:

PHP Manual > Function Reference > Variable and Type Related Extensions > Filter (English)

Scroll to Top