Open In App

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:

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.




<?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");
}
  
?>

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:




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

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


Article Tags :