filter_has_var Function: Check if Input Exists in PHP

PHP filter_has_var

User input does not always come through as expected. Sometimes values are missing or not set at all. This can lead to errors when your code tries to use that data. The filter_has_var() function helps avoid this. It checks if the input exists before your code uses it.

In this article you will learn how filter_has_var function works and its syntax. Also, you will see well-explained examples. Let’s get started.

What filter_has_var does

filter_has_var is a PHP function that checks if a variable is present in a specified input stream, such as $_GET, $_POST, or $_COOKIE. It returns true if the variable exists in the input stream and false if it does not.

This function helps you check if the input is available before you use it. Here is its syntax:

filter_has_var(int $type, string $variable_name);

So, the $type (int) tells PHP where to look for the input. It must be one of these constants:

  • INPUT_GET: Looks in the $_GET array (query string).
  • INPUT_POST: Looks in the $_POST array (form data).
  • INPUT_COOKIE: Looks in the $_COOKIE array.
  • INPUT_SERVER: Looks in the $_SERVER array.
  • INPUT_ENV: Looks in the $_ENV array.

While $variable_name (string) refers to the name of the variable you want to check. For example, "username" or "email".

Here is an example:

if (filter_has_var(INPUT_GET, 'username')) {
    echo "Username exists in the URL parameters!";
}

This checks if the "username" variable is present in the URL query string. If it exists, the message will display. If not, nothing happens.

filter_has_var() is useful because it lets you check if a variable exists in the user input before you use it. This prevents errors like “undefined index” notices when a variable is missing.

It also helps you avoid using unexpected or invalid input. It is especially helpful when you handle form data or URL parameters.

Examples of Different Inputs with filter_has_var

The filter_has_var() allows you check if a variable exists in different input types such as:

  • GET
  • POST
  • COOKIE.

This helps you handle user input and avoid errors across different request methods.

Use INPUT_GET when you need to check if a variable exists in the URL query string. This is common for values passed through links, like search terms or page numbers.

For example:

if (filter_has_var(INPUT_GET, 'id')) {
    echo "ID exists in the URL.";
}

The script checks if the id parameter exists in the URL, such as in example.com?page=2&id=123.

While the INPUT_POST checks for variables in the body of a POST request, like data submitted through a form.

Here is an example:

if (filter_has_var(INPUT_POST, 'email')) {
    echo "Email exists in the form submission.";
}

Here, the script checks if the email parameter is part of a form submission via POST.

You can also use the INPUT_COOKIE if you need to check if a variable exists in a cookie.

Here is an example:

if (filter_has_var(INPUT_COOKIE, 'user_session')) {
    echo "User session cookie exists.";
}

This example checks if a cookie named user_session exists.

You can also check for server variables (e.g., $_SERVER), which provide information about the environment or the current request.

if (filter_has_var(INPUT_SERVER, 'HTTP_USER_AGENT')) {
    echo "User-Agent header exists.";
}

This checks if the User-Agent header is set in the server request.

Wrapping Up

In this article, you learned how to use filter_has_var() to check for variables in different input streams like $_GET, $_POST, $_COOKIE, and $_SERVER.

It helps you to prevent errors and improve security to confirm that the data exists before processing it.

Here is a quick recap:

  • filter_has_var checks if a variable exists in a specified input stream.
  • It helps you prevent errors and improve code security.
  • You can use it with various request methods like GET and POST, etc.
  • It combines filter_has_var with filter_input() which helps you to make sure that input is both present and sanitized before processing it.

FAQ’s

What does filter_has_var do in PHP?

PHP filter_has_var checks if a specific variable exists in an input stream like $_GET, $_POST, or $_COOKIE. It returns true if the variable is present and false if it is not. This helps prevent errors from missing input values.

How do I check if a GET parameter exists using filter_has_var?

Use filter_has_var(INPUT_GET, 'param_name') to check if a GET parameter exists. For example:
if (filter_has_var(INPUT_GET, 'id')) {
    echo "ID exists in the URL.";
}
This ensures the id parameter is present before using it.

Can filter_has_var be used with form submissions?

Yes, you can check if a form field exists in a POST request using:
if (filter_has_var(INPUT_POST, 'email')) {
    echo "Email exists in the form submission.";
}
This prevents errors from undefined form inputs.

What is the difference between isset() and filter_has_var()?

isset() checks if a variable is set and not null, but filter_has_var() specifically checks for input data in predefined input streams (GET, POST, etc.), making it more suitable for user input validation.

Similar Reads

PHP array_intersect_ukey Function: How it Works with Examples

The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys…

PHP OOP Programming: A Complete Tutorial

A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…

PHP fwrite: How to Write Data to Files in PHP

actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…

PHP Filters: How to Validate and Sanitize User Inputs

User input can be unpredictable and dangerous. A simple form submission might carry malicious code or invalid data and lead…

PHP XML Expat Parser: A Complete Guide

If you've ever tried to pull data from an XML file, you know it can be a bit tricky at…

How to Connect PHP to MongoDB

Connecting PHP to MongoDB allows you to integrate database functionalities into your web applications. MongoDB is a NoSQL database that…

PHP array_key_first Function: How it Works with Examples

The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…

Mastering PHP’s Do-While Loop: Examples and Explanation

The PHP do-while loop is a type of loop that executes a block of code at least once, and then…

How to Remove the Last Character from a String in PHP

You may need to cut the last character from a string if it shows by mistake in PHP. That may…

PHP Namespace: How to Group Code (With Example)

PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…

Previous Article

PHP filter_var: Understand How to Sanitize Input

Next Article

History of PHP: From PHP/FI to Modern Web Development

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.