PHP filter_has_var() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filter

Definition

In PHP, filter_has_var() checks whether a variable was sent in a given input source, such as a GET query string, a POST form, a cookie, or the server environment.

It only answers yes or no. It does not read or validate the value, so it is a quick way to ask 'did the user send this field at all?' before you call filter_input().

Syntax

Call filter_has_var() with the values you need. Start with this line.

PHP
$sent = filter_has_var(INPUT_GET, 'email');
CallWhat it does
filter_has_var(INPUT_GET, 'email')Returns true if ?email=... was in the query string of the request.
filter_has_var(INPUT_POST, 'submit')Returns true if a POST field named submit was sent, for example by a form button.
filter_has_var(INPUT_COOKIE, 'theme')Returns true if the browser sent a cookie named theme.

The first argument must be one of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. An empty value still counts as 'set', so ?email= returns true.

Library declarations
PHP
filter_has_var(int $type, string $var_name): bool

Parameters

ParameterDescription
typeRequired. One of the input constants: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
var_nameRequired. The name of the variable to look for, for example 'email'.

Return Value

Return ValueDescription
booltrue if the variable exists in the chosen input source, otherwise false.

PHP Examples

Example 1 — Check If a Query Variable Was Sent

Ask whether the email field exists in the GET data. When you run this from the command line there is no query string, so the answer is false.

PHP
<?php
if (filter_has_var(INPUT_GET, 'email')) {
    echo "The email field was sent.", PHP_EOL;
} else {
    echo "The email field was not sent.", PHP_EOL;
}

How It Works

  1. INPUT_GET tells PHP to look at the query string of the request (the part after ?).
  2. filter_has_var() checks whether a variable named email exists there.
  3. In a browser, visiting page. [email protected] would make the first branch run.
  4. On the command line there is no query string, so the else branch prints the message.

Example 2 — Changing $_GET Does Not Count

Setting a value in $_GET by hand does not change what filter_has_var() sees, because it reads the original request.

PHP
<?php
$_GET['email'] = '[email protected]';

echo "In \$_GET: ", isset($_GET['email']) ? "yes" : "no", PHP_EOL;
echo "filter_has_var: ", filter_has_var(INPUT_GET, 'email') ? "yes" : "no", PHP_EOL;

How It Works

  1. The script writes a value into the $_GET array by hand.
  2. isset() reads the array, so it finds the email key.
  3. filter_has_var() looks at the real request data that PHP received, which did not include email.
  4. This is why filter functions are safer: they ignore changes made later in your own code.

Example 3 — Detect a Submitted Form

Use filter_has_var() with INPUT_POST to decide whether a form was submitted before validating anything.

PHP
<?php
if (!filter_has_var(INPUT_POST, 'submit')) {
    echo "Form not submitted yet.", PHP_EOL;
    exit;
}

$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
echo $email ? "Thanks, $email" : "Please enter a valid email.", PHP_EOL;

How It Works

  1. The form button would be named submit, so its presence tells us the form was posted.
  2. filter_has_var(INPUT_POST, 'submit') returns false when nothing was posted.
  3. The script prints a message and stops with exit before doing any validation.
  4. When a real form is posted, filter_input() with FILTER_VALIDATE_EMAIL would check the email.

Frequently Asked Questions

It checks if a variable exists in a specific input source such as GET, POST, COOKIE, SERVER, or ENV. It returns true or false and does not look at the value itself.
Not exactly. isset() reads the $_GET array, which your code can change. filter_has_var() checks the original request data, so later changes to the array do not affect it.
Yes. If the URL is page.php?email= the variable exists with an empty string, so filter_has_var(INPUT_GET, 'email') returns true. Use filter_input() to check the actual value.
You can use INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, and INPUT_ENV. Request-wide arrays like $_REQUEST are not supported.
The command line has no query string, POST body, or cookies, so there are no GET, POST, or COOKIE variables to find. It works as expected when a web server handles the request.

Next: filter_id()

filter_id() turns the name of a filter, such as 'validate_email', into its numeric ID, for.

filter_id() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful