PHP | filter_has_var() function

The filter_has_var() function is an inbuilt function in PHP which is used to check whether the variable available or not especially it checks if a variable of a specified input type exist or not. It returns True on success or False on failure.

Syntax:

bool filter_has_var( $type, $variable_name )

Parameters: This function accepts two parameters as mentioned above and described below:



  • type: It is the required parameter and used to specify the type of input to check. The possible input types are INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV.
  • variable_name: It is the required parameter and used to specify the name of a variable which need to check.

Return Value: It returns True on success or False on failure.

Note: This function is available for PHP 5.2.0 and later versions.

Example 1: In this example, the input variable “name” is sent to the PHP page.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate 
// filter_has_var() function
  
if(!filter_has_var(INPUT_GET, "name")) {
    echo("Input type does not exist");
}
else {
    echo("Input type exists");
}
  
?>

chevron_right


Output: This example may not show “Input type exists” as output in online IDE as there is no option for sending parameter with the code. So run it somewhere on server or localhost. If the name input type defined and sent through GET method so !filter_has_var(INPUT_GET, “name”) returning false and printing output as “Input type exists”.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
if (!filter_has_var(INPUT_GET, "email")) {
    echo("Email not found");
} else {
    echo("Email found");
}
?>

chevron_right


Output: This example will not show expected output in online IDE as they do not allow to run PHP code with GET parameters. So, run it on some other hosting server or in localhost. As email input type defined and sent through GET method so !filter_has_var(INPUT_GET, “email”) returning false and printing output as “Email found”.

References: http://php.net/manual/en/function.filter-has-var.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.