filter_has_var(): Check if variable is specified type

Check an external input with filter_has_var()

filter_has_var() checks whether an input variable exists in the selected external input source such as INPUT_GET or INPUT_POST.

if (filter_has_var(INPUT_GET, 'id')) {
    echo 'id was supplied';
}
<?php
$book_id=100; // Direct variable , not through GET 
if(filter_has_var(INPUT_GET,'book_id')){
echo "Yes true " . $_GET['book_id'];
}else{
echo "No false";
}
?>
Output ( DEMO )
Click here to pass the book_id value through GET method and check the output below.

Click again for without GET variable.
No false $_GET['book_id']
What is GET and POST method of passing data ?
filter_has_var function checks the variable of specified type is available or not. The same result can be achieved by using isset() function also but the advantage of filter_has_var is it checks the specified type of input ( source ) is available or not.
If we expect the variable to be available by POST method and not by GET method for form posting then the same can be checked by using filter_has_var function in PHP.

Here is the syntax
bool filter_has_var(TYPE, variable_name);
The possible type of availability are
INPUT_POST, 
INPUT_GET, 
INPUT_COOKIE,
INPUT_SERVER, 
INPUT_ENV

Demo using POST

Book id=

No false $_POST['book_id']
Source code of above demo is here
<?Php
echo "<pre><code>";
$book_id=100; // Direct variable , not through GET 
if(filter_has_var(INPUT_POST,'book_id')){
echo "Yes true " . $_POST['book_id'];
}else{
echo "No false";
}
echo "</code></pre>";
?>
fillter_has_var is a better way than isset() function to to check the existence of a variable.

Questions

Filter reference Validating Boolean data Ctype_alnum to check alphanumeric characters Validating Email address


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer